Home > Article > Backend Development > Implement a two-way queue with PHP_PHP tutorial
1. Introduction
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, which restricts insertion and deletion operations to both ends of the table. A deque (double-ended queue) is like a queue, but you can add or remove elements from either end.
Reference: http://zh.wikipedia.org/zh-cn/%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97
2.PHP implementation code
<span class</span><span DoubleQueue { </span><span public</span> <span $queue</span> = <span array</span><span (); </span><span /*</span><span *(尾部)入队 *</span><span */</span> <span public</span> <span function</span> push(<span $value</span><span ) { </span><span return</span> <span array_push</span>(<span $this</span>->queue,<span $value</span><span ); } </span><span /*</span><span *(尾部)出队*</span><span */</span> <span public</span> <span function</span><span pop() { </span><span return</span> <span array_pop</span>(<span $this</span>-><span queue); } </span><span /*</span><span *(头部)入队*</span><span */</span> <span public</span> <span function</span> enq(<span $value</span><span ) { </span><span return</span> <span array_unshift</span>(<span $this</span>->queue,<span $value</span><span ); } </span><span /*</span><span *(头部)出队*</span><span */</span> <span public</span> <span function</span><span deq() { </span><span return</span> <span array_shift</span>(<span $this</span>-><span queue); } </span><span /*</span><span *清空队列*</span><span */</span> <span public</span> <span function</span><span empty() { </span><span unset</span>(<span $this</span>-><span queue); } </span><span /*</span><span *获取列头*</span><span */</span> <span public</span> <span function</span><span first() { </span><span return</span> <span reset</span>(<span $this</span>-><span queue); } </span><span /*</span><span * 获取列尾 *</span><span */</span> <span public</span> <span function</span><span last() { </span><span return</span> <span end</span>(<span $this</span>-><span queue); } </span><span /*</span><span * 获取长度 *</span><span */</span> <span public</span> <span function</span><span length() { </span><span return</span> <span count</span>(<span $this</span>-><span queue); } } </span>