ホームページ  >  記事  >  バックエンド開発  >  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 までご連絡ください。