首页 > 语言 > PHP > 正文

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

2024-09-04 11:46:34
字体:大 中 小
来源:转载
供稿:网友

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

例子代码如下:

  1. <?php 
  2. /** 
  3. *@php模拟  队列 
  4. */ 
  5. class Queue 
  6. { 
  7.  private $myQueue;  //队列容器 
  8.  private $size ;     //队列的长度 
  9.  public function __construct() 
  10.  { 
  11.   $this->myQueue=array(); 
  12.   $this->size=0; 
  13.  } 
  14.  /** 
  15.  *@入栈操作 
  16.  */ 
  17.  public function putQueue($data) 
  18.  { 
  19.   $this->myQueue[$this->size++]=$data; 
  20.   return $this; //开源软件:Vevb.com 
  21.  } 
  22.  /** 
  23.  *@出栈 
  24.  */ 
  25.  public function getQueue() 
  26.  { 
  27.   if(!$this->isEmpty()) 
  28.   { 
  29.                     $front=array_splice($this->myQueue,0,1); 
  30.                     $this->size--; 
  31.       return $front[0]; 
  32.   } 
  33.   return false; 
  34.  } 
  35.  /** 
  36.  *@ 获取全部的消息队列 
  37.  */ 
  38.  public function allQueue() 
  39.  { 
  40.   return $this->myQueue; 
  41.  } 
  42.  /** 
  43.  *@ 获取队列的表态 
  44.  */ 
  45.  public function frontQueue() 
  46.  { 
  47.   if(!$this->isEmpty()) 
  48.   { 
  49.    return $this->myQueue[0]; 
  50.   } 
  51.   return false; 
  52.  } 
  53.  /** 
  54.  *@ 返回队列的长度 
  55.  */ 
  56.  public function getSize() 
  57.  { 
  58.   return $this->size; 
  59.  } 
  60.    public function isEmpty() 
  61.    { 
  62.      return 0===$this->size; 
  63.    } 
  64. } 
  65. ?>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表