PHP使用数组实现队列我们只要用到 rray_push()和array_pop()两个系统函数来完成了,下面一起来看看吧,希望例子对各位有帮助。
例子
代码如下 | 复制代码 |
/** *@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; } /** *@出栈 */ 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; } } ?> |