当前位置:   article > 正文

【数据结构初阶】队列

【数据结构初阶】队列

hello!

目录

一、概念与结构

二、队列的实现

Queue.h

Queue.c

test.c


一、概念与结构

1、概念:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的特性。

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

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

2、队列底层结构的选择

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

二、队列的实现

Queue.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<assert.h>
  4. #include<stdlib.h>
  5. #include<stdbool.h>
  6. //定义队列结点的结构
  7. typedef int QDataType;
  8. typedef struct QueueNode
  9. {
  10. QDataType data;
  11. struct QueueNode* next;
  12. }QueueNode;
  13. //定义队列的结构
  14. typedef struct Queue
  15. {
  16. struct QueueNode* phead;
  17. struct QueueNode* ptail;
  18. int size; //保存队列有效数据的个数
  19. }Queue;
  20. //初始化
  21. void QueueInit(Queue* pq);
  22. //入队列
  23. void QueuePush(Queue* pq,QDataType x);
  24. //判空
  25. bool QueueEmpty(Queue* pq);
  26. //出队列
  27. void QueuePop(Queue* pq);
  28. //取队头数据
  29. QDataType QueueFront(Queue* pq);
  30. //取队尾数据
  31. QDataType QueueBack(Queue* pq);
  32. //队列有效元素个数
  33. int QueueSize(Queue* pq);
  34. //销毁队列
  35. void QueueDestroy(Queue* pq);

Queue.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include"Queue.h"
  3. //初始化
  4. void QueueInit(Queue* pq)
  5. {
  6. assert(pq);
  7. pq->phead = pq->ptail = NULL;
  8. pq->size = 0;
  9. }
  10. //入队列
  11. void QueuePush(Queue* pq, QDataType x)
  12. {
  13. assert(pq);
  14. //申请新结点
  15. QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
  16. if (newnode == NULL)
  17. {
  18. perror("malloc fail!");
  19. exit(1);
  20. }
  21. newnode->data = x;
  22. newnode->next = NULL;
  23. if (pq->phead == NULL)
  24. {
  25. //若队列为空
  26. pq->phead = pq->ptail = newnode;
  27. }
  28. else
  29. {
  30. //若队列不为空
  31. pq->ptail->next = newnode;
  32. pq->ptail = newnode;
  33. }
  34. pq->size++;
  35. }
  36. //判空
  37. bool QueueEmpty(Queue* pq)
  38. {
  39. assert(pq);
  40. return pq->phead == NULL;
  41. }
  42. //出队列
  43. void QueuePop(Queue* pq)
  44. {
  45. assert(pq);
  46. assert(!QueueEmpty(pq));
  47. //若队列里只有一个结点,避免ptail变成野指针
  48. if (pq->phead == pq->ptail)
  49. {
  50. free(pq->phead);
  51. pq->phead = pq->ptail = NULL;
  52. }
  53. else
  54. {
  55. //队列不止一个结点
  56. QueueNode* next = pq->phead->next;
  57. free(pq->phead);
  58. pq->phead = next;
  59. }
  60. --pq->size;
  61. }
  62. //取队头数据
  63. QDataType QueueFront(Queue* pq)
  64. {
  65. assert(pq);
  66. assert(!QueueEmpty(pq));
  67. return pq->phead->data;
  68. }
  69. //取队尾数据
  70. QDataType QueueBack(Queue* pq)
  71. {
  72. assert(pq);
  73. assert(!QueueEmpty(pq));
  74. return pq->ptail->data;
  75. }
  76. //队列有效元素个数
  77. int QueueSize(Queue* pq)
  78. {
  79. assert(pq);
  80. return pq->size;
  81. }
  82. //销毁队列
  83. void QueueDestroy(Queue* pq)
  84. {
  85. assert(pq);
  86. assert(!QueueEmpty(pq));
  87. QueueNode* pcur = pq->phead;
  88. while (pcur)
  89. {
  90. QueueNode* next = pcur->next;
  91. free(pcur);
  92. pcur = next;
  93. }
  94. pq->ptail = pq->phead = NULL;
  95. pq->size = 0;
  96. }

test.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include"Queue.h"
  3. void QueueTest01()
  4. {
  5. Queue q;
  6. QueueInit(&q);
  7. QueuePush(&q, 1);
  8. QueuePush(&q, 2);
  9. QueuePush(&q, 3);
  10. QueuePush(&q, 4);
  11. QueuePop(&q);
  12. /*QueuePop(&q);
  13. QueuePop(&q);
  14. QueuePop(&q);*/
  15. printf("head:%d\n",QueueFront(&q));
  16. printf("tail:%d\n",QueueBack(&q));
  17. printf("size:%d\n",QueueSize(&q));
  18. QueueDestroy(&q);
  19. }
  20. int main()
  21. {
  22. QueueTest01();
  23. return 0;
  24. }

我是云边有个稻草人

期待与你的下一次相遇!

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

闽ICP备14008679号