当前位置:Gxlcms > PHP教程 > php数据结构实现队列的示例代码

php数据结构实现队列的示例代码

时间:2021-07-01 10:21:17 帮助过:15人阅读

队列(Queue): 满足先进先出(FIFO)的规则;

下面使用php实现一个简单的循环队列模型;

初始状态的队列,队列长度为0,队头和队尾的指针相同均位于队列的开始;

入队操作:队尾指针向后移动,长度加一;

出队操作:队头指针向后移动,长度减一;

循环队列特点:队列大小固定,队列所开辟的内存空间可循环使用,指针的移动是靠与queueSize取余运算移动;

下面的例子是利用数组实现队列存储,数组下标作为指针;

  1. <?php
  2. /**
  3. * Class Queue
  4. */
  5. class Queue
  6. {
  7. /**
  8. * @var int 队头指针
  9. */
  10. private $_front;
  11. /**
  12. * @var int 队尾指针
  13. */
  14. private $_rear;
  15. /**
  16. * @var array 队列数组
  17. */
  18. private $_queue;
  19. /**
  20. * @var int 队列实际长度
  21. */
  22. private $_queueLength;
  23. /**
  24. * @var int 队列容量;
  25. */
  26. private $_queueSize;
  27. /**
  28. * Queue constructor.初始化队列
  29. * @param int $capacity 容量(循环队列的最大长度)
  30. */
  31. public function __construct($size)
  32. {
  33. $this->_queue = [];
  34. $this->_queueSize = $size;
  35. $this->_front = 0;
  36. $this->_rear = 0;
  37. $this->_queueLength = 0;
  38. }
  39. /**
  40. * 销毁队列;
  41. */
  42. public function __destruct()
  43. {
  44. unset($this->_queue);
  45. }
  46. /**
  47. * @method 入队
  48. * @param mixed $elem 入队的元素
  49. * @return bool
  50. */
  51. public function enQueue($elem)
  52. {
  53. if (!$this->isFull()) {
  54. $this->_queue[$this->_rear] = $elem;
  55. $this->_rear++;
  56. $this->_rear = $this->_rear % $this->_queueCapacity;
  57. $this->_queueLength++;
  58. return true;
  59. }
  60. return false;
  61. }
  62. /**
  63. * @method 出队
  64. * @return mixed|null
  65. */
  66. public function deQueue()
  67. {
  68. if (!$this->isEmpty()) {
  69. $elem = $this->_queue[$this->_front];
  70. //unset($this->_queue[$this->_front]);
  71. $this->_front++;
  72. $this->_front %= $this->_queueCapacity;
  73. $this->_queueLength--;
  74. return $elem;
  75. }
  76. return null;
  77. }
  78. /**
  79. * @method 判断队列是否为空;
  80. * @return bool
  81. */
  82. public function isEmpty()
  83. {
  84. return $this->_queueLength === 0;
  85. }
  86. /**
  87. * @method 判断队列是否饱和;
  88. * @return bool
  89. */
  90. public function isFull()
  91. {
  92. return $this->_queueLength === $this->_queueCapacity;
  93. }
  94. /**
  95. * @method 遍历队列并
输出(测试队列) */ public function outputQueue() { for ($i = $this->_front; $i < $this->_queueLength + $this->_front; $i++) { echo $this->_queue[$i % $this->_queueCapacity].PHP_EOL; } } /** * @method 清空队列 */ public function clearQueue() { $this->_queue = []; $this->_front = 0; $this->_rear = 0; $this->_queueLength = 0; } }

测试队列类,讲道理是没什么大问题的,优化就靠真实的业务场景了;

  1. $a = new Queue(3);
  2. echo 'enQueue1 $a->enQueue(1)'.PHP_EOL;
  3. $a->enQueue(1);
  4. echo 'enQueue2 $a->enQueue(2)'.PHP_EOL;
  5. $a->enQueue(2);
  6. echo 'enQueue3 $a->enQueue(3)'.PHP_EOL;
  7. $a->enQueue(3);
  8. echo 'enQueue4 $a->enQueue(4)'.PHP_EOL;
  9. $a->enQueue(4); //讲道理是失败的;
  10. $a->outputQueue(); //
输出 1 2 3 echo PHP_EOL; echo PHP_EOL; echo $a->deQueue(); //输出 1 队列 2 3 echo PHP_EOL; echo PHP_EOL; echo $a->deQueue(); //输出 2 队列 3 $a->enQueue(5); //队列 3 5 echo PHP_EOL; echo PHP_EOL; $a->outputQueue(); //输出 3 5 $a->clearQueue(); //队列空;

如有不对,敬请指教

以上就是php数据结构实现队列的示例代码的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行