PHP雙向隊列實現代碼

來源:互聯網
上載者:User

1,什麼是雙向隊列

deque,全名double-ended queue,是一種具有隊列和棧的性質的資料結構。雙端隊列中的元素可以從兩端彈出,其限定插入和刪除操作在表的兩端進行。雙向隊列(雙端隊列)就像是一個隊列,但是可以在任何一端添加或移除元素。

參考:http://zh.wikipedia.org/zh-cn/%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97

2,php實現雙向隊列的代碼

  1. class DoubleQueue

  2. {
  3. public $queue = array();
  4. /**(尾部)入隊 **/
  5. public function addLast($value)
  6. {
  7. return array_push($this->queue,$value);
  8. }
  9. /**(尾部)出隊**/
  10. public function removeLast()
  11. {
  12. return array_pop($this->queue);
  13. }
  14. /**(頭部)入隊**/
  15. public function addFirst($value)
  16. {
  17. return array_unshift($this->queue,$value);
  18. }
  19. /**(頭部)出隊**/
  20. public function removeFirst()
  21. {
  22. return array_shift($this->queue);
  23. }
  24. /**清空隊列**/
  25. public function makeEmpty()
  26. {
  27. unset($this->queue);
  28. }
  29. /**擷取列頭**/
  30. public function getFirst()
  31. {
  32. return reset($this->queue);
  33. }

  34. /** 擷取列尾 **/

  35. public function getLast()
  36. {
  37. return end($this->queue);
  38. }

  39. /** 擷取長度 **/

  40. public function getLength()
  41. {
  42. return count($this->queue);
  43. }
  44. }

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.