<?php
/**
* php队列算法
*
* Create On 2010-6-4
* Author Been
* QQ:281443751
* Email:binbin1129@126.com
**/
class data {
//数据
private $data;

public function __construct($data){
$this->data=$data;
echo $data.":哥进队了!<br>";
}

public function getData(){
return $this->data;
}
public function __destruct(){
echo $this->data.":哥走了!<br>";
}
}
class queue{
protected $front;//队头
protected $rear;//队尾
protected $queue=array('0'=>'队尾');//存储队列
protected $maxsize;//最大数

public function __construct($size){
$this->initQ($size);
}
//初始化队列
private function initQ($size){
$this->front=0;
$this->rear=0;
$this->maxsize=$size;
}
//判断队空
public function QIsEmpty(){
return $this->front==$this->rear;
}
//判断队满
public function QIsFull(){
return ($this->front-$this->rear)==$this->maxsize;
}
//获取队首数据
public function getFrontDate(){
return $this->queue[$this->front]->getData();
}
//入队
public function InQ($data){
if($this->QIsFull())echo $data.":我一来咋就满了!(队满不能入队,请等待!)<br>";
else {
$this->front++;
for($i=$this->front;$i>$this->rear;$i--){
//echo $data;
if($this->queue[$i])unset($this->queue[$i]);
$this->queue[$i]=$this->queue[$i-1];
}
$this->queue[$this->rear+1]=new data($data);
//print_r($this->queue);
//echo $this->front;
echo '入队成功!<br>';
}
}
//出队
public function OutQ(){
if($this->QIsEmpty())echo "队空不能出队!<br>";
else{
unset($this->queue[$this->front]);
$this->front--;
//print_r($this->queue);
//echo $this->front;
echo "出队成功!<br>";
}
}
}
$q=new queue(3);
$q->InQ("小苗");
$q->InQ('马帅');
$q->InQ('溜冰');
$q->InQ('张世佳');
$q->OutQ();
$q->InQ("周瑞晓");
$q->OutQ();
$q->OutQ();
$q->OutQ();
$q->OutQ();

转自: http://www.cnblogs.com/huangtaozi/p/3758815.html

最新文章

  1. 由用友NC刷新功能得到启示
  2. 创建 Transact-SQL 作业步骤
  3. SqlServer基础:Bit类型
  4. apache配置文件 httpd-vhosts.conf 和 htaccess
  5. 稀疏表示(sparse representation)和字典学习
  6. python时间函数
  7. CCF计算机认证注意事项
  8. CentOS和Redhat发行版linux内核版本的对应关系
  9. 关于SGA与memory_target 大小冲突照成数据库无法挂载问题
  10. 【尚学堂&#183;Hadoop学习】MapReduce案例1--天气
  11. Jenkins编辑或替换All view
  12. pyadb关于python操作adb的资料
  13. P1108 低价购买
  14. Spring的IOC/DI使用到的技术
  15. Synchronized、lock、volatile、ThreadLocal、原子性总结、Condition
  16. Oracle 函数function之返回结果集
  17. Delphi中的三目运算函数有哪些?(XE10.2+WIN764)
  18. SpringBoot 入门(一)
  19. kissy初体验-waterfall
  20. Educational Codeforces Round 54 (Rated for Div. 2) DE

热门文章

  1. hibernate+spring mvc, 解决hibernate 对象懒加载 json序列化问题
  2. 引用、数组引用与指针引用、内联函数inline、四种类型转换运算符
  3. Mysql中的算术运算符详解
  4. blender, merge顶点
  5. spring概念简介、bean扫描与注册实现方式
  6. 关于搭配junit 和JUnit报initializationError的解决方法
  7. jQuery select添加图标
  8. AIX中查找端口号和进程
  9. 回文串dp
  10. 教程-Delphi中的GExperts搜索代码快捷键