php 队列类
来源:互联网 发布:监控找不到网络主机 编辑:程序博客网 时间:2024/05/22 08:29
<?php/** * PHP Class for queue * @author yangqijun@live.cn * @copyright DataFrog Beijingbei Ltd. 2011-07-25 */ class Queue { public $length=12; //默认队列,相当于初始化队列 public $queue = array(); // if String like this "22,23,24" convert to array to do queue public $delimiter=','; function __construct($queue=array()) { $this->queue=$queue; } /** * @desc start queue * @param String $param new queue element */ public function run($param) { if(!is_array($this->queue)){ $this->strToQue();//将数组视为队列 } $currentlength=$this->countqueue(); //Count the queue length echo $currentlength; echo $this->length.'<br>'; if($currentlength<$this->length&&$this->length>0) { $this->queAdd($param); }else if($this->length==0) //如果为空队列,则将队列初始化为输入的队列 { $param=empty($param)?0:$param; $this->queue[]=$param; } else { for ($i=0;$i<$currentlength-$this->length-1;$i++)//队列比规定的队列多,要删掉队首的元素,才能入队 { $this->queRemove(); } $this->queAdd($param); } return $this->queue; } /** * String like this "22,23,24" convert to array to do queue * @param String $string * @param String $delimiter */ public function strToQue (){ if (empty($this->queue)) { $this->queue=array(); } else { $this->queue=explode($this->delimiter,$this->queue); } } /** * insert $node into queue * @param string $node */ private function queAdd($node){ array_push($this->queue,$node); $this->countqueue(); } private function queRemove(){ $node = array_shift($this->queue); $this->countqueue(); return $node; } private function countqueue(){ $currentlength= count($this->queue); return $currentlength; } function __destruct() { unset($this->queue); } } //example $str='88|89|90|56|23|45|69|23|20|100'; $obj=new Queue ($str); $obj->length=8; // 队列元素长度 $obj->delimiter='|'; //如果队列是字符串,则元素直接的分隔符为| $a=$obj->run('91'); //要添加到队列中的元素 $a=$obj->run('92'); $a=$obj->run('93'); $a=$obj->run('94'); print_r($a);?>
0 0
- php 双向队列类
- php 队列类
- PHP 实现队列类 仅供参考
- php memcache类(memcache队列)
- PHP 队列
- php-队列
- PHP队列
- PHP 队列
- php 队列、堆栈类V1.1
- php自己实现memcached的队列类
- php 封装Memcache 队列缓存类
- php 封装Memcache 队列缓存类
- php实现的消息队列类
- php算法----队列
- php队列实验
- php之任务队列
- php 简单双向队列
- php RabbitMQ消息队列
- maven的使用方法
- php基础知识
- 一款陌生人状态社交软件--same
- 【剑指offer-Java版】06重建二叉树
- 十条jQuery代码片段助力Web开发效率提升
- php 队列类
- (礼拜一log)前端开发:children和find函数;position属性
- 消息推送测试 远程通知
- qt button以及label实现不规则图形
- 如何实现基于内容和用户画像的个性化推荐
- Android Material Design 兼容库的使用详解
- 大钉骑马走江湖
- Android内存管理、监测剖析
- adb install *.apk时,出现INSTALL_FAILED_MISSING_SHARED_LIBRARY错误的解决方法