PHP two-way queue, dual-end queue code

Source: Internet
Author: User
PHP two-way queue, dual-end queue code

  1. /**
  2. * User: jifei
  3. * Date: 2013-07-30
  4. * Time: 23: 12
  5. */
  6. /**
  7. * PHP implements two-way queues and dual-end queues
  8. * A double-ended queue (deque) is a data structure with queues and stacks.
  9. * Elements in a dual-end queue can pop up from both ends. the insert and delete operations are limited to both sides of the queue.
  10. */
  11. Class Deque
  12. {
  13. Public $ queue = array ();
  14. /**
  15. * Constructor initializes the queue.
  16. */
  17. Public function _ construct ($ queue = array ())
  18. {
  19. If (is_array ($ queue ))
  20. {
  21. $ This-> queue = $ queue;
  22. }
  23. }
  24. /**
  25. * Obtain the first element.
  26. */
  27. Public function front ()
  28. {
  29. Return reset ($ this-> queue );
  30. }
  31. /**
  32. * Obtain the last element.
  33. */
  34. Public function back ()
  35. {
  36. Return end ($ this-> queue );
  37. }
  38. /**
  39. * Determines whether it is null.
  40. */
  41. Public function is_empty ()
  42. {
  43. Return empty ($ this-> queue );
  44. }
  45. /**
  46. * Queue size
  47. */
  48. Public function size ()
  49. {
  50. Return count ($ this-> queue );
  51. }
  52. /**
  53. * Insert to the end
  54. */
  55. Public function push_back ($ val)
  56. {
  57. Array_push ($ this-> queue, $ val );
  58. }
  59. /**
  60. * Insert to header
  61. */
  62. Public function push_front ($ val)
  63. {
  64. Array_unshift ($ this-> queue, $ val );
  65. }
  66. /**
  67. * Remove the last element.
  68. */
  69. Public function pop_back ()
  70. {
  71. Return array_pop ($ this-> queue );
  72. }
  73. /**
  74. * Remove the first element.
  75. */
  76. Public function pop_front ()
  77. {
  78. Return array_shift ($ this-> queue );
  79. }
  80. /**
  81. * Clear a queue
  82. */
  83. Public function clear ()
  84. {
  85. $ This-> queue = array ();
  86. }
  87. }
  88. // Initialize a two-way queue
  89. $ Deque = new Deque (array (1, 2, 3, 4, 5 ));
  90. Echo $ deque-> size (). PHP_EOL;
  91. Echo $ deque-> is_empty (). PHP_EOL;
  92. Echo $ deque-> front (). PHP_EOL;
  93. Echo $ deque-> back (). PHP_EOL;
  94. Echo PHP_EOL;
  95. // Pop-up element test
  96. Echo $ deque-> pop_back (). PHP_EOL;
  97. Echo $ deque-> pop_front (). PHP_EOL;
  98. Echo $ deque-> size (). PHP_EOL;
  99. Echo PHP_EOL;
  100. $ Deque-> push_back ('A'). PHP_EOL;
  101. $ Deque-> push_front (0). PHP_EOL;
  102. Echo PHP_EOL;
  103. // Insert test
  104. Echo $ deque-> front (). PHP_EOL;
  105. Echo $ deque-> back (). PHP_EOL;
  106. Echo $ deque-> size (). PHP_EOL;
  107. Echo PHP_EOL;
  108. // Clear the test
  109. $ Deque-> clear ();
  110. Echo $ deque-> is_empty ();


PHP

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.