资讯专栏INFORMATION COLUMN

实战PHP数据结构基础之队列

cnio / 2104人阅读

摘要:环形队列为充分利用向量空间,克服假溢出现象的方法是将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量。双端队列截止目前我们所实现的队列都是在队尾入队,队首出队的结构。

什么是队列

队列是另外一种遵循先进先出原则的线性数据结构。队列有两端可供操作,一端出队,一端入队。这个特点和栈不同,栈只有一端可以用来操作。入队总是在后端,出队在前端。

常见操作

enqueue -> 入队

dequeue -> 出队

peek -> 返回队列前端元素

isEmpty -> 是否为空

PHP实现

首先我们定义一个QueueInterface。

</>复制代码

  1. interface QueueInterface
  2. {
  3. public function enqueue(string $item);
  4. public function dequeue();
  5. public function isEmpty();
  6. public function peek();
  7. }

来看基于数组的队列实现

</>复制代码

  1. class ArrQueue implements QueueInterface
  2. {
  3. private $queue;
  4. private $limit;
  5. public function __construct(int $limit = 0)
  6. {
  7. $this->limit = $limit;
  8. $this->queue = [];
  9. }
  10. public function isEmpty()
  11. {
  12. return empty($this->queue);
  13. }
  14. public function dequeue()
  15. {
  16. if ($this->isEmpty()) {
  17. throw new UnderflowException("queue is empty");
  18. } else {
  19. array_shift($this->queue);
  20. }
  21. }
  22. public function enqueue(string $item)
  23. {
  24. if (count($this->queue) >= $this->limit) {
  25. throw new OverflowException("queue is full");
  26. } else {
  27. array_unshift($this->queue, $item);
  28. }
  29. }
  30. public function peek()
  31. {
  32. return current($this->queue);
  33. }
  34. }

得益于PHP强大的array结构,我们轻而易举的写出来了队列的基本操作方法。果然世界上最好的语言名不虚传。

可能机智的同学已经猜到了,我之前已经定义了一个队列接口,那队列的实现肯定不止只有上面一种哈。来看基于链表的实现。

</>复制代码

  1. class LinkedListQueue implements QueueInterface
  2. {
  3. private $limit;
  4. private $queue;
  5. public function __construct(int $limit = 0)
  6. {
  7. $this->limit = $limit;
  8. $this->queue = new LinkedList();
  9. }
  10. public function isEmpty()
  11. {
  12. return $this->queue->getSize() == 0;
  13. }
  14. public function peek()
  15. {
  16. return $this->queue->getNthNode(0)->data;
  17. }
  18. public function enqueue(string $item)
  19. {
  20. if ($this->queue->getSize() < $this->limit) {
  21. $this->queue->insert($item);
  22. } else {
  23. throw new OverflowException("queue is full");
  24. }
  25. }
  26. public function dequeue()
  27. {
  28. if ($this->isEmpty()) {
  29. throw new UnderflowException("queue is empty");
  30. } else {
  31. $lastItem = $this->peek();
  32. $this->queue->deleteFirst();
  33. return $lastItem;
  34. }
  35. }
  36. }

里面涉及到了之前的链表实现,不了解细节的同学可以去这里看看。

Spl中的队列

强大的PHP已经内置了队列实现,可以使用的方法和上面我们自己实现的类似。

</>复制代码

  1. class SqlQueue
  2. {
  3. private $splQueue;
  4. public function __construct()
  5. {
  6. $this->splQueue = new SplQueue();
  7. }
  8. public function enqueue(string $data = null)
  9. {
  10. $this->splQueue->enqueue($data);
  11. }
  12. public function dequeue()
  13. {
  14. return $this->splQueue->dequeue();
  15. }
  16. }
优先队列

优先队列是一种特殊的队列,入队或者出队的顺序都是基于每个节点的权重。

顺序序列

优先队列可以分为有序优先队列和无序优先队列。有序优先队列又有两种情况,倒序或者顺序。在顺序序列中,我们可以迅速的找出最大或者最小优先级别的节点,复杂度是O(1)。但是插入的话会花费掉更多的时间,因为我们要检查每一个节点的优先级别然后插入到合适的位置。

无序序列

在无序序列中,在插入新节点的时候我们不需要根据他们的优先级确定位置。入队的时候像普通队列一样,插入到队列的末尾。但是当我们想移除优先级最高的元素的时候,我们要扫描每一个节点来确定移除优先级最高的那一个。接下来我们还是基于链表实现一个顺序的优先队列。

</>复制代码

  1. class LinkedListPriorityQueue
  2. {
  3. private $limit;
  4. private $queue;
  5. public function __construct(int $limit = 0)
  6. {
  7. $this->limit = $limit;
  8. $this->queue = new LinkedList();
  9. }
  10. public function enqueue(string $data = null, int $priority)
  11. {
  12. if ($this->queue->getSize() > $this->limit) {
  13. throw new OverflowException("Queue is full");
  14. } else {
  15. $this->queue->insert($data, $priority);
  16. }
  17. }
  18. public function dequeue(): string
  19. {
  20. if ($this->isEmpty()) {
  21. throw new UnderflowException("Queue is empty");
  22. } else {
  23. $item = $this->peek();
  24. $this->queue->deleteFirst();
  25. return $item->data;
  26. }
  27. }
  28. public function peek()
  29. {
  30. return $this->queue->getNthNode(0);
  31. }
  32. public function isEmpty()
  33. {
  34. return $this->queue->getSize() === 0;
  35. }
  36. }
环形队列

为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量。存储在其中的队列称为循环队列。环形队列也是一种数组,只是它在逻辑上把数组的头和尾相连,形成循环队列,当数组尾满的时候,要判断数组头是否为空,不为空继续存放数据。

</>复制代码

  1. class CircularQueue implements QueueInterface
  2. {
  3. private $queue;
  4. private $limit;
  5. private $front = 0;
  6. private $rear = 0;
  7. public function __construct(int $limit = 0)
  8. {
  9. $this->limit = $limit;
  10. $this->queue = [];
  11. }
  12. public function isEmpty()
  13. {
  14. return $this->front === $this->rear;
  15. }
  16. public function isFull()
  17. {
  18. $diff = $this->rear - $this->front;
  19. if ($diff == -1 || $diff == ($this->limit - 1)) {
  20. return true;
  21. }
  22. return false;
  23. }
  24. public function peek()
  25. {
  26. return $this->queue[$this->front];
  27. }
  28. public function dequeue()
  29. {
  30. if ($this->isEmpty()) {
  31. throw new UnderflowException("Queue is empty");
  32. }
  33. $item = $this->queue[$this->front];
  34. $this->queue[$this->front] = null;
  35. $this->front = ($this->front + 1) % $this->limit;
  36. return $item;
  37. }
  38. public function enqueue(string $item)
  39. {
  40. if ($this->isFull()) {
  41. throw new OverflowException("Queue is full");
  42. }
  43. $this->queue[$this->rear] = $item;
  44. $this->rear = ($this->rear + 1) % $this->limit;
  45. }
  46. }
双端队列

截止目前我们所实现的队列都是在队尾(rear)入队,队首(front) 出队的结构。在特殊的情况下,我们希望不论是队首还是队尾都可以入队或者出队,这种结构就叫做双端队列。基于我们之前实现的链表结构,我们可以轻而易举的实现这样的结构。

</>复制代码

  1. class LinkedListDeQueue
  2. {
  3. private $limit = 0;
  4. private $queue;
  5. public function __construct(int $limit = 0)
  6. {
  7. $this->limit = $limit;
  8. $this->queue = new DataStructureLinkedListLinkedList();
  9. }
  10. public function dequeueFromFront(): string
  11. {
  12. if ($this->isEmpty()) {
  13. throw new UnderflowException("Queue is empty");
  14. }
  15. $item = $this->queue->getNthNode(0);
  16. $this->queue->deleteFirst();
  17. return $item->data;
  18. }
  19. public function dequeueFromBack(): string
  20. {
  21. if ($this->isEmpty()) {
  22. throw new UnderflowException("Queue is empty");
  23. }
  24. $item = $this->queue->getNthNode($this->queue->getSize() - 1);
  25. $this->queue->deleteLast();
  26. return $item->data;
  27. }
  28. public function isFull()
  29. {
  30. return $this->queue->getSize() >= $this->limit;
  31. }
  32. public function enqueueAtBack(string $data = null)
  33. {
  34. if ($this->isFull()) {
  35. throw new OverflowException("queue is full");
  36. }
  37. $this->queue->insert($data);
  38. }
  39. public function enqueueAtFront(string $data = null)
  40. {
  41. if ($this->isFull()) {
  42. throw new OverflowException("queue is full");
  43. }
  44. $this->queue->insertAtFirst($data);
  45. }
  46. public function isEmpty()
  47. {
  48. return $this->queue->getSize() === 0;
  49. }
  50. public function peekFront()
  51. {
  52. return $this->queue->getNthNode(0)->data;
  53. }
  54. public function peekBack()
  55. {
  56. return $this->queue->getNthNode($this->queue->getSize() - 1)->data;
  57. }
  58. }

里面涉及到了之前的链表实现,不了解细节的同学可以去这里看看。

总结

栈和队列是我们最常用的数据结构,我们会在其他的复杂数据结构中看到这两种抽象数据类型的应用。在下一章,我们继续学习PHP数据结构之递归,这是一种将复杂问题简单化的常用思路。

专题系列

PHP基础数据结构专题系列目录地址:地址 主要使用PHP语法总结基础的数据结构和算法。还有我们日常PHP开发中容易忽略的基础知识和现代PHP开发中关于规范、部署、优化的一些实战性建议,同时还有对Javascript语言特点的深入研究。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28865.html

相关文章

  • 实战PHP数据结构基础

    摘要:栈和队列栈和队列和之前讲到的实战数据结构基础之双链表一样都是线性结构。来看基于数组的栈实现得益于强大的结构,我们轻而易举的写出来了栈的基本操作方法。专题系列基础数据结构专题系列目录地址主要使用语法总结基础的数据结构和算法。 栈和队列 栈和队列和之前讲到的实战PHP数据结构基础之双链表 一样都是线性结构。 栈有什么特点 栈遵循后进先出的原则(LIFO)。这意味着栈只有一个出口用来压入元素...

    banana_pi 评论0 收藏0
  • PHP基础

    摘要:分别为适配器模式,装饰器模式,代理模式,外观模式,桥接模式,组合模式,享元模式。设计模式五适配器模式适配器模式将某个对象的接生成器和协程的实现在这篇文章中,作者针对那些比较难以理解的概念,以一个更为通俗的方式去讲明白。。 PHP 源码注解 PHP 的详细源码注解 PHP 字符串操作整理 一些有关字符串的常用操作。 Redis 常见七种使用场景 (PHP 实战) 这篇文章主要介绍利用 R...

    HtmlCssJs 评论0 收藏0
  • PHPer书单

    摘要:想提升自己,还得多看书多看书多看书下面是我收集到的一些程序员应该看得书单及在线教程,自己也没有全部看完。共勉吧当然,如果你有好的书想分享给大家的或者觉得书单不合理,可以去通过进行提交。讲师温铭,软件基金会主席,最佳实践作者。 想提升自己,还得多看书!多看书!多看书!下面是我收集到的一些PHP程序员应该看得书单及在线教程,自己也没有全部看完。共勉吧!当然,如果你有好的书想分享给大家的或者...

    jimhs 评论0 收藏0
  • PHP小知识点

    摘要:那些琐碎的知识点作者记录的的很奇特很难记的知识点。易错知识点整理注意和的区别中和都是输出的作用,但是两者之间还是有细微的差别。今天手头不忙,总结一下,分享过程中掌握的知识点。 深入理解 PHP 之:Nginx 与 FPM 的工作机制 这篇文章从 Nginx 与 FPM 的工作机制出发,探讨配置背后的原理,让我们真正理解 Nginx 与 PHP 是如何协同工作的。 PHP 那些琐碎的知识...

    hover_lew 评论0 收藏0
  • 实战PHP数据结构基础双链表

    摘要:什么是双链表上一篇实战数据结构基础之单链表说到单链表由一个一个的作为节点的对象构成的,每一个节点都有指向下一个节点的指针,最后一个节点的指针域指向空。 什么是双链表? 上一篇实战PHP数据结构基础之单链表说到 单链表由一个一个的作为节点的对象构成的,每一个节点都有指向下一个节点的指针,最后一个节点的指针域指向空。每个节点可以存储任何数据类型。 而双链表每个节点有两个指针域,分别指向前驱...

    Michael_Lin 评论0 收藏0

发表评论

0条评论

cnio

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<