PHP bidirectional queue implementation code

Source: Internet
Author: User
PHP bidirectional queue implementation code

1. what is a bidirectional queue?

Deque, full name double-ended queue, is a data structure with queue and stack properties. Elements in a dual-end queue can pop up from both ends, and the limited insert and delete operations are performed at both ends of the table. A two-way queue (dual-end queue) is like a queue, but elements can be added or removed at any end.

Reference: http://zh.wikipedia.org/zh-cn/%E5%8F%8C%E7% AB %AF%E9%98%9F%E5%88%97

2. php implements two-way queue code

  1. Class DoubleQueue

  2. {
  3. Public $ queue = array ();
  4. /** (Tail) team up **/
  5. Public function addLast ($ value)
  6. {
  7. Return array_push ($ this-> queue, $ value );
  8. }
  9. /** (Tail) team-OUT **/
  10. Public function removeLast ()
  11. {
  12. Return array_pop ($ this-> queue );
  13. }
  14. /** (Header) team up **/
  15. Public function addFirst ($ value)
  16. {
  17. Return array_unshift ($ this-> queue, $ value );
  18. }
  19. /** (Head) team-OUT **/
  20. Public function removeFirst ()
  21. {
  22. Return array_shift ($ this-> queue );
  23. }
  24. /** Clear the queue **/
  25. Public function makeEmpty ()
  26. {
  27. Unset ($ this-> queue );
  28. }
  29. /** Get the column header **/
  30. Public function getFirst ()
  31. {
  32. Return reset ($ this-> queue );
  33. }

  34. /** Get the end of a column **/

  35. Public function getLast ()
  36. {
  37. Return end ($ this-> queue );
  38. }

  39. /** Get length **/

  40. Public function getLength ()
  41. {
  42. Return count ($ this-> queue );
  43. }
  44. }

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.