微信扫一扫打赏支持

使用PHP实现双向队列

使用PHP实现双向队列

一、总结

就是几个array函数

 push pop 

shift unshift

 

n. 移动;变化;手段;轮班

vi. 移动;转变;转换

vt. 转移;改变;替换

 

二、使用PHP实现双向队列

 1 /**
 2  * Class Deque
 3  * 使用PHP实现双向队列
 4  */
 5 class Deque{
 6     private $queue = array();
 7     public function addFirst($item){//头入队
 8         array_unshift($this->queue,$item);
 9     }
10     public function addLast($item){//尾入队
11         array_push($this->queue,$item);
12     }
13     public function removeFirst(){//头出队
14         array_shift($this->queue);
15     }
16     public function removeLast(){//尾出队
17         array_pop($this->queue);
18     }
19     public  function show(){//打印
20         foreach($this->queue as $item){
21             echo $item." ";
22         }
23         echo "<br/>";
24     }
25 }
26 $deque = new Deque();
27 $deque->addFirst(2);
28 $deque->addLast(3);
29 $deque->addLast(4);
30 $deque->addFirst(5);
31 $deque->show();

 

posted @ 2018-05-12 00:40  范仁义  阅读(608)  评论(0)    收藏  举报