A queue is a linear table that is based on the FIFO principle:
See how each language implements the queue:
PHP Implementation queue: First element as Team head, last element as team tail
- <? PHP
- /**
- * The queue is so simple
- *
- * @link http://www.phpddt.com
- */
- $array = array(' PHP ', ' JAVA ');
- Array_push($array, ' PYTHON '); //into the queue
- Array_shift($array); //Out queue
What is a double-ended queue (or two-way queue) Deque, full name double-ended queue?
That is, the elements can be queued or out of the queue in any part of the queues, if we call these methods Insertleft () and Insertright (), and Removeleft () and Removeright (). If you strictly prohibit calling the Insertleft () and Removeleft () methods (or disabling the right segment), the dual-ended queue function is the same as the stack. Calling Insertleft () and Removeright () (or the opposite of another method) is forbidden, and it functions like a queue. A dual-ended queue is a multi-purpose data structure compared to a stack or queue.
PHP implements a dual-ended queue:
- <? PHP
- Class Deque
- {
- public $queue = array();
- /** (tail) queue **/
- public function addlast($value)
- {
- return array_push($this, queue,$value);
- }
- /** (tail) out of the team **/
- public function removelast()
- {
- return array_pop($this,queue);
- }
- /** (head) queue **/
- public function addfirst($value)
- {
- return array_unshift($this, queue,$value);
- }
- /** (head) out of the team **/
- public function removefirst()
- {
- return array_shift($this,queue);
- }
- /** emptying the queue **/
- public function makeempty()
- {
- unset($this,queue);
- }
- /** Get column header **/
- public function getfirst()
- {
- return Reset($this-toqueue);
- }
- /** get end of column **/
- public function getlast()
- {
- return end($this,queue);
- }
- /** Get length **/
- public function getlength()
- {
- return Count($this,queue);
- }
- }
Purpose of the queue:
Queues are a good way to handle data transfer and storage asynchronously, and when you frequently insert data into a database, and frequently submit data to a search engine, you can take a queue to insert asynchronously. In addition, the slow processing logic, the processing logic with concurrent quantity limit, can be processed in the background through message queue, such as FLV video conversion, sending SMS, sending e-mail, etc.
Reprint Please specify address: http://www.phpddt.com/php/queue.html respect for the work of others is to respect their own!
PHP implementation Queue and queue principle