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
-
-
class DoubleQueue - {
- public $queue = array();
-
- /**(Tail)Enter the queue**/
- public function addLast($value)
- {
- return array_push($this->queue,$value);
- }
- /**(Tail) Dequeue**/
- public function removeLast()
- {
- return array_pop($this->queue);
- }
- /* *(head) join the team**/
- public function addFirst($value)
- {
- return array_unshift($this->queue,$value);
- }
- /**(head) dequeue**/
- public function removeFirst()
- {
- return array_shift($this->queue);
- }
- /**Clear queue**/
- public function makeEmpty()
- {
- unset($this->queue);
- }
-
- /**Get column header**/
- public function getFirst()
- {
- return reset($this->queue);
- }
/**Get column tail **/
- public function getLast()
- {
- return end($this->queue);
- }
/**Get length **/
- public function getLength()
- {
- return count($this-> ;queue);
- }
-
- }
-
Copy code
|