Home  >  Article  >  Backend Development  >  PHP实现双向队列算法代码

PHP实现双向队列算法代码

WBOY
WBOYOriginal
2016-06-20 13:01:04890browse

PHP实现双向队列算法代码

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);
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn