PHP bidirectional queue, double-ended queue code

Source: Internet
Author: User
  1. /**
  2. * User:jifei
  3. * date:2013-07-30
  4. * Time:23:12
  5. */
  6. /**
  7. * PHP implementation of two-way queue, double-ended queue
  8. * Double-ended queue (deque, full name double-ended queue) is a data structure with queue and stack properties.
  9. * Elements in a double-ended queue can be ejected from both ends, and insert and delete operations are scoped to both sides of the queue.
  10. */
  11. Class Deque
  12. {
  13. Public $queue =array ();
  14. /**
  15. * Constructor Initialization queue
  16. */
  17. Public function __construct ($queue =array ())
  18. {
  19. if (Is_array ($queue))
  20. {
  21. $this->queue= $queue;
  22. }
  23. }
  24. /**
  25. * Get the first element
  26. */
  27. Public Function Front ()
  28. {
  29. Return Reset ($this->queue);
  30. }
  31. /**
  32. * Get last Element
  33. */
  34. Public Function back ()
  35. {
  36. Return End ($this->queue);
  37. }
  38. /**
  39. * Determine if it is empty
  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 Tail
  54. */
  55. Public Function push_back ($val)
  56. {
  57. Array_push ($this->queue, $val);
  58. }
  59. /**
  60. * Insert to Head
  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. * Empty 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. Empty test
  109. $deque->clear ();
  110. echo $deque->is_empty ();
Copy Code
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.