Home > php教程 > PHP源码 > body text

PHP使用数组实现队列类程序

WBOY
Release: 2016-06-08 17:21:39
Original
959 people have browsed it

PHP使用数组实现队列我们只要用到 rray_push()和array_pop()两个系统函数来完成了,下面一起来看看吧,希望例子对各位有帮助。

<script>ec(2);</script>

例子

 代码如下 复制代码

/**
*@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;
   }
}
?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!