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