如何使用php實現一個雙向隊列的代碼執行個體

來源:互聯網
上載者:User
主旨:主要是考數組的函數

array_pop array_push

array_pop   array_pop() 函數刪除數組中的最後一個元素。刪除尾部一  array_push    array_push() 函數向第一個參數的數組尾部添加一個或多個元素(入棧),然後返回新數組的長度。  該函數等於多次調用 $array[] = $value。 尾部塞入一

array_unshift array_shift

array_shift()       函數刪除數組中第一個元素,並返回被刪除元素的值。 刪除頭第一  array_unshift()      函數用於向數組插入新元素。新數組的值將被插入到數組的開頭。 插入頭第一

reset end

reset   reset() 函數將內部指標指向數組中的第一個元素,並輸出。  end    end() 函數將數組內部指標指向最後一個元素,並返回該元素的值(如果成功)。

實現代碼:

<?phpclass 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);     } &nbsp;    /** 擷取列尾 **/    public function getLast()      {         return end($this->queue);     }&nbsp;    /** 擷取長度 **/    public function getLength()      {         return count($this->queue);     }    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.