当前位置:   article > 正文

[数据结构]队列

[数据结构]队列

1.队列的概念及结构

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

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

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

2.队列的实现

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。

以下是队列的创建:

空队列:

空队列插入第一个元素:

入队列和出队列

接下来是代码实现,这里我还是选用比较容易实现的链表来实现队列,个人认为类似单链表.

代码示例:

  1. //Queue.h
  2. #pragma once
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<stdbool.h>
  6. #include<assert.h>
  7. typedef int QDataType;
  8. typedef struct QueueNode
  9. {
  10. int val;
  11. struct QueueNode* next;
  12. }QNode;
  13. typedef struct Queue
  14. {
  15. QNode* phead;
  16. QNode* ptail;
  17. int size;
  18. }Queue;
  19. void QueueInit(Queue* pq);
  20. void QueueDestroy(Queue* pq);
  21. // 入队列
  22. void QueuePush(Queue* pq, QDataType x);
  23. // 出队列
  24. void QueuePop(Queue* pq);
  25. QDataType QueueFront(Queue* pq);
  26. QDataType QueueBack(Queue* pq);
  27. bool QueueEmpty(Queue* pq);
  28. int QueueSize(Queue* pq);
  1. //Queue.c
  2. #include"Queue.h"
  3. void QueueInit(Queue* pq)
  4. {
  5. assert(pq);
  6. pq->phead = NULL;
  7. pq->ptail = NULL;
  8. pq->size = 0;
  9. }
  10. void QueueDestroy(Queue* pq)
  11. {
  12. assert(pq);
  13. QNode* cur = pq->phead;
  14. while (cur)
  15. {
  16. QNode* next = cur->next;
  17. free(cur);
  18. cur = next;
  19. }
  20. pq->phead = pq->ptail = NULL;
  21. pq->size = 0;
  22. }
  23. // 入队列
  24. void QueuePush(Queue* pq, QDataType x)
  25. {
  26. assert(pq);
  27. QNode* newnode = (QNode*)malloc(sizeof(QNode));
  28. if (newnode == NULL)
  29. {
  30. perror("malloc fail");
  31. return;
  32. }
  33. newnode->val = x;
  34. newnode->next = NULL;
  35. if (pq->ptail)
  36. {
  37. pq->ptail->next = newnode;
  38. pq->ptail = newnode;
  39. }
  40. else
  41. {
  42. pq->phead = pq->ptail = newnode;
  43. }
  44. pq->size++;
  45. }
  46. // 出队列
  47. void QueuePop(Queue* pq)
  48. {
  49. assert(pq);
  50. assert(pq->phead != NULL);
  51. if (pq->phead->next == NULL)
  52. {
  53. free(pq->phead);
  54. pq->phead = pq->ptail = NULL;
  55. }
  56. else
  57. {
  58. QNode* next = pq->phead->next;
  59. free(pq->phead);
  60. pq->phead = next;
  61. }
  62. pq->size--;
  63. }
  64. QDataType QueueFront(Queue* pq)
  65. {
  66. assert(pq);
  67. // 暴力检查
  68. assert(pq->phead != NULL);
  69. return pq->phead->val;
  70. }
  71. QDataType QueueBack(Queue* pq)
  72. {
  73. assert(pq);
  74. // 暴力检查
  75. assert(pq->ptail != NULL);
  76. return pq->ptail->val;
  77. }
  78. bool QueueEmpty(Queue* pq)
  79. {
  80. assert(pq);
  81. return pq->size == 0;
  82. }
  83. int QueueSize(Queue* pq)
  84. {
  85. assert(pq);
  86. return pq->size;
  87. }
  1. #include"Queue.h"
  2. int main()
  3. {
  4. Queue q;
  5. QueueInit(&q);
  6. QueuePush(&q, 1);
  7. QueuePush(&q, 2);
  8. printf("%d ", QueueFront(&q));
  9. QueuePop(&q);
  10. QueuePush(&q, 3);
  11. QueuePush(&q, 4);
  12. while (!QueueEmpty(&q))
  13. {
  14. printf("%d ", QueueFront(&q));
  15. QueuePop(&q);
  16. }
  17. QueueDestroy(&q);
  18. return 0;
  19. }

运行结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/202131?site
推荐阅读
相关标签
  

闽ICP备14008679号