注册

PHP工程师面试题之双向队列的实现代码

嗨豆壳技术文章 时间:2015-04-11 13:54:43 [ray]

接上一篇文章,还是“拍黄片”工程师面试题。本文分享PHP如何实现双向队列。正在面试的童鞋,工程师们。面试搞不定?快打开嗨豆壳。。马上破解之。哈哈哈

<?php 
class deque
{
	public $queue  = array();
	public $length = 0;
   
	public function frontAdd($node){
		array_unshift($this->queue,$node);
		$this->countqueue();
	}
	public function frontRemove(){
		$node = array_shift($this->queue);
		$this->countqueue();
		return $node;
	}
	  
	public function rearAdd($node){
		array_push($this->queue,$node);
		$this->countqueue();
	}
	 
	public function rearRemove(){
		$node = array_pop($this->queue);
		$this->countqueue();
		return $node;
	}
	 
	public function countqueue(){
		$this->length = count($this->queue);    
	}
}
// 测试用,面试答题可不写以下部分
$fruit = new deque();
echo $fruit -> length;
$fruit -> frontAdd("Apple");
$fruit -> rearAdd("Watermelon");
print_r($fruit);
?>

冒泡、二分算法题://www.hi-docs.com/article/detail-MTMy.html

标签

本文地址:https://www.hi-docs.com/article/133.html

上一篇:PHP工程师面试题之算法题-二分查找、冒泡排序

下一篇:百度免费IP地址查询API,查询接口