PHP implementation Queue and queue principle

Source: Internet
Author: User

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

  1. <? PHP
  2. /**
  3. * The queue is so simple
  4. *
  5. * @link http://www.phpddt.com
  6. */
  7. $array = array(' PHP ', ' JAVA ');
  8. Array_push($array, ' PYTHON '); //into the queue
  9. 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:

  1. <? PHP
  2. Class Deque
  3. {
  4. public $queue = array();
  5. /** (tail) queue **/
  6. public function addlast($value)
  7. {
  8. return array_push($this, queue,$value);
  9. }
  10. /** (tail) out of the team **/
  11. public function removelast()
  12. {
  13. return array_pop($this,queue);
  14. }
  15. /** (head) queue **/
  16. public function addfirst($value)
  17. {
  18. return array_unshift($this, queue,$value);
  19. }
  20. /** (head) out of the team **/
  21. public function removefirst()
  22. {
  23. return array_shift($this,queue);
  24. }
  25. /** emptying the queue **/
  26. public function makeempty()
  27. {
  28. unset($this,queue);
  29. }
  30. /** Get column header **/
  31. public function getfirst()
  32. {
  33. return Reset($this-toqueue);
  34. }
  35. /** get end of column **/
  36. public function getlast()
  37. {
  38. return end($this,queue);
  39. }
  40. /** Get length **/
  41. public function getlength()
  42. {
  43. return Count($this,queue);
  44. }
  45. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.