PHP实现-一个双向队列

2018-08-22 14:17 By "Powerless" 3006 0 1

单向队列:只能从头进,从尾出

$array =  array('PHP', 'JAVA');
array_push($array, 'PYTHON'); //入队列
array_shift($array); //出队列


双向队列:头尾都可以进出

class Deque{
    private $queue=array();
    function addFirst($item){//头入队
        return array_unshift($this->queue,$item);
    }
    function addLast($item){//尾入队
        return array_push($this->queue,$item);
    }
    function removeFirst(){//头出队
        return array_shift($this->queue);
    }
    function removeLast(){//尾出队
        return array_pop($this->queue);
    }
    function show(){//显示
        echo implode(" ",$this->queue);
    }
    function clear(){//清空
        unset($this->queue);
    }
    function getFirst(){
        return array_shift($this->queue);
    }
    function getLast(){
        return array_pop($this->queue);
    }
    function getLength(){
        return count($this->queue);
    }
}
$q=new Deque();
$q->addFirst(1);
$q->addLast(5);
$q->removeFirst();
$q->removeLast();
$q->addFirst(2);
$q->addLast(4);
$q->show();


评 论

Others Discussion

  • 一些常见的基础概念
    Posted on 2018-11-28 19:10
  • HTTP头中隐藏PHP版本号
    Posted on 2021-01-11 16:38
  • QPS、TPS、RT、吞吐量到底是什么
    Posted on 2020-02-02 01:15
  • MySQL事务介绍
    Posted on 2019-06-05 18:14
  • 能创建多少个 TCP 连接?
    Posted on 2021-08-02 16:00
  • MySQL中的行级锁,表级锁,页级锁
    Posted on 2018-08-25 11:00
  • 2018年云计算热词
    Posted on 2019-06-12 18:19
  • PHP 基金会来啦!
    Posted on 2022-10-08 17:40