PHP實現雙端隊列

PHP實現隊列:第一個元素作爲隊頭,最後一個元素作爲隊尾

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

PHP實現雙端隊列

<?php
class Deque{
    public $queue=array();
    //尾入列
    public function addLast($value){
        return array_push($this->queue,$value);
    }
    //尾出列
    public function removeLast(){
        return array_pop($this->queue);
    }
    //頭入列
    public function addFirst($value){
        return array_unshift($this->queue,$value);
    }
    //頭出列
    public function removeFirst(){
        return array_shift($this->queue);
    }
    //清空隊列
    public function makeEmpty(){
        unset($this->queue);
    }
    //獲取列頭
    public function getFirst(){
        return reset($this->queue);
    }
}
發佈了38 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章