>  기사  >  백엔드 개발  >  PHP实现双向队列算法代码

PHP实现双向队列算法代码

WBOY
WBOY원래의
2016-06-20 13:01:04890검색

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


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.