史上最强数据结构----轻松拿捏队列及队列的模拟实现

x33g5p2x  于2022-04-07 转载在 其他  
字(2.2k)|赞(0)|评价(0)|浏览(618)

1.队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出 FIFO(First In First Out)

入队列:进行插入操作的一端称为队尾

出队列:进行删除操作的一端称为队头

2.队列的实现

队列可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低,因为要从后面向前进行覆盖数据。

队列图示结构:

注释:由于队列的实现和链表的实现很多都是一样的!因为此处队列就是由链表实现的,队列的插入就是链表的尾插,队列的删除就是链表的头删,并没有太多的新意,在此前的链表的模拟实现中已经做出详细的讲解,此处不再多做缀解!希望大家能够自己模拟实现,如果有不懂的可以参照一下丸丸的代码,当然丸丸的代码只供参照,还有优化的空间,比如加上哨兵位之类的!

Queue.h头文件:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4. #include<assert.h>
  5. typedef int QDataType;
  6. typedef struct QueueNode
  7. {
  8. QDataType data;
  9. struct QueueNode* next;
  10. }QNode;
  11. typedef struct Queue
  12. {
  13. QNode* head;
  14. QNode* tail;
  15. }Queue;
  16. void QueueInit(Queue* pq);
  17. void QueueDestory(Queue* pq);
  18. void QueuePush(Queue*pq,QDataType x);
  19. void QueuePop(Queue*pq);
  20. size_t QueueSize(Queue* pq);
  21. QDataType QueueFront(Queue* pq);
  22. QDataType QueueBack(Queue* pq);
  23. bool QueueEmpty(Queue* pq);

Queue.c源文件

  1. #include"Queue.h"
  2. void QueueInit(Queue* pq)//队列的初始化
  3. {
  4. assert(pq);
  5. pq->head =pq->tail = NULL;
  6. }
  7. QNode* BuyQNode(QDataType x)//开辟一个节点
  8. {
  9. QNode* newnode = (QNode*)malloc(sizeof(QNode));
  10. if (newnode == NULL)
  11. {
  12. printf("fail malloc\n");
  13. exit(-1);
  14. }
  15. newnode->data = x;
  16. newnode->next = NULL;
  17. return newnode;
  18. }
  19. void QueuePush(Queue* pq, QDataType x)//入队
  20. {
  21. assert(pq);
  22. QNode* newnode = BuyQNode(x);
  23. if (pq->tail == NULL)//没有节点的时候
  24. {
  25. assert(pq->head == NULL);
  26. pq->head= pq->tail = newnode;
  27. }
  28. else
  29. {
  30. pq->tail->next = newnode;
  31. pq->tail = newnode;
  32. }
  33. }
  34. void QueuePop(Queue* pq)//出队
  35. {
  36. assert(pq);
  37. assert(pq->tail&&pq->head);
  38. if (pq->head->next==NULL)//处理只有一个节点的时候
  39. {
  40. free(pq->tail);
  41. pq->tail = pq->head = NULL;
  42. }
  43. else//有多个节点
  44. {
  45. QNode* next = pq->head->next;
  46. free(pq->head);
  47. pq->head = next;
  48. }
  49. }
  50. size_t QueueSize(Queue* pq)//求队列的元素数目
  51. {
  52. assert(pq);
  53. size_t size = 0;
  54. QNode* cur = pq->head;
  55. while (cur!= pq->tail->next)
  56. {
  57. size++;
  58. cur = cur->next;
  59. }
  60. return size;
  61. }
  62. QDataType QueueFront(Queue* pq)//求队列首节点存储的元素
  63. {
  64. assert(pq);
  65. assert(pq->head);
  66. return pq->head->data;
  67. }
  68. QDataType QueueBack(Queue* pq)//求队列尾节点存储的元素
  69. {
  70. assert(pq);
  71. assert(pq->tail);
  72. return pq->tail->data;
  73. }
  74. void QueueDestory(Queue* pq)//销毁队列
  75. {
  76. assert(pq);
  77. QNode* cur = pq->head;
  78. while (cur)
  79. {
  80. QNode* next = cur->next;
  81. free(cur);
  82. cur = next;
  83. }
  84. pq->head = pq->tail = NULL;
  85. }
  86. bool QueueEmpty(Queue* pq)//判断队列是否为空
  87. {
  88. assert(pq);
  89. return pq->head==NULL&&pq->tail==NULL;
  90. }

相关文章