PHP two-way queue implementation code

WBOY
Release: 2016-07-25 09:13:11
Original
988 people have browsed it

1. What is a two-way queue

Deque, the full name is double-ended queue, is a data structure with the properties of queue and stack. Elements in a double-ended queue can be popped from both ends, and insertion and deletion operations are limited to both ends of the table. A deque (double-ended queue) is like a queue, but elements can be added or removed from either end.

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

2, PHP code to implement two-way queue

  1. class DoubleQueue

  2. {
  3. public $queue = array();
  4. /**(Tail)Enter the queue**/
  5. public function addLast($value)
  6. {
  7. return array_push($this->queue,$value);
  8. }
  9. /**(Tail) Dequeue**/
  10. public function removeLast()
  11. {
  12. return array_pop($this->queue);
  13. }
  14. /* *(head) join the team**/
  15. public function addFirst($value)
  16. {
  17. return array_unshift($this->queue,$value);
  18. }
  19. /**(head) dequeue**/
  20. public function removeFirst()
  21. {
  22. return array_shift($this->queue);
  23. }
  24. /**Clear queue**/
  25. public function makeEmpty()
  26. {
  27. unset($this->queue);
  28. }
  29. /**Get column header**/
  30. public function getFirst()
  31. {
  32. return reset($this->queue);
  33. }

  34. /**Get column tail **/

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

  39. /**Get length **/

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

Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template