zoukankan      html  css  js  c++  java
  • php队列算法[转]

    <?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

  • 相关阅读:
    COGS 577 蝗灾 线段树+CDQ分治
    BZOJ 1305 二分+网络流
    BZOJ 1066 Dinic
    BZOJ 3544 treap (set)
    BZOJ 3940 AC自动机
    BZOJ 1503 treap
    BZOJ 3172 AC自动机
    BZOJ 2553 AC自动机+矩阵快速幂 (神题)
    BZOJ1901 ZOJ2112 线段树+treap (线段树套线段树)
    BZOJ 3196 线段树套平衡树
  • 原文地址:https://www.cnblogs.com/wherein/p/7201593.html
Copyright © 2011-2022 走看看