PHP two-way queue, double-ended queue code

WBOY
Release: 2016-07-25 08:42:17
Original
1250 people have browsed it
  1. /**
  2. * User: jifei
  3. * Date: 2013-07-30
  4. * Time: 23:12
  5. */
  6. /**
  7. * PHP implements two-way queue, double-ended queue
  8. * A double-ended queue (deque, full name double-ended queue) is a data structure with queue and stack properties.
  9. * Elements in a double-ended queue can be popped from both ends, and insertion and deletion operations are limited to both sides of the queue.
  10. */
  11. class Deque
  12. {
  13. public $queue=array();
  14. /**
  15. * Constructor initializes the queue
  16. */
  17. public function __construct($queue=array())
  18. {
  19. if(is_array($queue))
  20. {
  21. $this->queue=$queue;
  22. }
  23. }
  24. /**
  25. * Get the first element
  26. */
  27. public function front()
  28. {
  29. return reset($this->queue);
  30. }
  31. /**
  32. * Get the last element
  33. */
  34. public function back()
  35. {
  36. return end($this->queue);
  37. }
  38. /**
  39. * Determine whether it is empty
  40. */
  41. public function is_empty()
  42. {
  43. return empty($this->queue);
  44. }
  45. /**
  46. * Queue size
  47. */
  48. public function size()
  49. {
  50. return count($this->queue);
  51. }
  52. /**
  53. * Insert to end
  54. */
  55. public function push_back($val)
  56. {
  57. array_push($this->queue,$val);
  58. }
  59. /**
  60. *Insert to the end
  61. */
  62. public function push_front($val)
  63. {
  64. array_unshift($this->queue,$val);
  65. }
  66. /**
  67. * Remove the last element
  68. */
  69. public function pop_back()
  70. {
  71. return array_pop($this->queue);
  72. }
  73. /**
  74. * Remove the first element
  75. */
  76. public function pop_front()
  77. {
  78. return array_shift($this->queue);
  79. }
  80. /**
  81. * Clear the queue
  82. */
  83. public function clear()
  84. {
  85. $this->queue=array();
  86. }
  87. }
  88. //初始化一个双向队列
  89. $deque=new Deque(array(1,2,3,4,5));
  90. echo $deque->size().PHP_EOL;
  91. echo $deque->is_empty().PHP_EOL;
  92. echo $deque->front().PHP_EOL;
  93. echo $deque->back().PHP_EOL;
  94. echo PHP_EOL;
  95. //弹出元素测试
  96. echo $deque->pop_back().PHP_EOL;
  97. echo $deque->pop_front().PHP_EOL;
  98. echo $deque->size().PHP_EOL;
  99. echo PHP_EOL;
  100. $deque->push_back('a').PHP_EOL;
  101. $deque->push_front(0).PHP_EOL;
  102. echo PHP_EOL;
  103. //插入测试
  104. echo $deque->front().PHP_EOL;
  105. echo $deque->back().PHP_EOL;
  106. echo $deque->size().PHP_EOL;
  107. echo PHP_EOL;
  108. //清空测试
  109. $deque->clear();
  110. echo $deque->is_empty();
复制代码

PHP


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