PHP bidirectional Queue Implementation code

Source: Internet
Author: User

1, what is a two-way queue

Deque, full name double-ended queue, is a data structure that has the nature of queues and stacks. Elements in a double-ended queue can be ejected from both ends, and their qualifying insert and delete operations are performed at both ends of the table. A two-way queue (a double-ended queue) is like a queue, but an element can be added or removed at either end.

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

2,php implementing two-way queuing code

  1. Class Doublequeue

  2. {
  3. Public $queue = Array ();
  4. /** (tail) queue **/
  5. Public Function AddLast ($value)
  6. {
  7. Return Array_push ($this->queue, $value);
  8. }
  9. /** (tail) out of the team **/
  10. Public Function Removelast ()
  11. {
  12. Return Array_pop ($this->queue);
  13. }
  14. /** (head) queue **/
  15. Public Function AddFirst ($value)
  16. {
  17. Return Array_unshift ($this->queue, $value);
  18. }
  19. /** (head) out of the team **/
  20. Public Function Removefirst ()
  21. {
  22. Return Array_shift ($this->queue);
  23. }
  24. /** emptying the queue **/
  25. Public Function Makeempty ()
  26. {
  27. unset ($this->queue);
  28. }
  29. /** Get column Header **/
  30. Public Function GetFirst ()
  31. {
  32. Return Reset ($this->queue);
  33. }

  34. /** get end of 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. }

Copy Code
  • 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.