PHP使用數組實現隊列我們只要用到 rray_push()和array_pop()兩個系統函數來完成了,下面一起來看看吧,希望例子對各位有幫助.
例子代碼如下:
- <?php
- /**
- *@php模擬 隊列
- */
- class Queue
- {
- private $myQueue; //隊列容器
- private $size ; //隊列的長度
- public function __construct()
- {
- $this->myQueue=array();
- $this->size=0;
- }
- /**
- *@入棧操作
- */
- public function putQueue($data)
- {
- $this->myQueue[$this->size++]=$data;
- return $this; //開源軟件:Vevb.com
- }
- /**
- *@出棧
- */
- public function getQueue()
- {
- if(!$this->isEmpty())
- {
- $front=array_splice($this->myQueue,0,1);
- $this->size--;
- return $front[0];
- }
- return false;
- }
- /**
- *@ 獲取全部的消息隊列
- */
- public function allQueue()
- {
- return $this->myQueue;
- }
- /**
- *@ 獲取隊列的表態
- */
- public function frontQueue()
- {
- if(!$this->isEmpty())
- {
- return $this->myQueue[0];
- }
- return false;
- }
- /**
- *@ 返回隊列的長度
- */
- public function getSize()
- {
- return $this->size;
- }
- public function isEmpty()
- {
- return 0===$this->size;
- }
- }
- ?>
新聞熱點
疑難解答