• 【数据结构】C语言实现队列


    目录

    前言

    1. 队列

    1.1 队列的概念

    1.2 队列的结构

    2. 队列的实现

    2.1 队列的定义

    2.2 队列的初始化

    2.3 入队

    2.4 出队

    2.5 获取队头元素

    2.6 获取队尾元素

    2.7 判断空队列

    2.8 队列的销毁

    3. 队列完整源码

    Queue.h

    Queue.c


    • 🎈个人主页:库库的里昂
    •  🎐C/C++领域新星创作者
    •  🎉欢迎 👍点赞✍评论⭐收藏
    • ✨收录专栏:数据结构与算法
    • 🤝希望作者的文章能对你有所帮助,有不足的地方请在评论区留言指正,大家一起学习交流!🤗

    前言

    在前几期的学习中,我们认识了顺序表、链表和栈这三种线性表,而在本期学习中,我们将会认识别的线性表。跟随我们的脚本,看看队列有怎样的特点。

    1. 队列

    1.1 队列的概念

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

    • 入队列:进行插入操作的一端称为队尾
    • 出队列:进行删除操作的一端称为队头
    1.2 队列的结构

    2. 队列的实现

    2.1 队列的定义

    在入队时相当于尾插,我们可以定义一个尾指针来记录尾的位置。这就使我们传指针时,要传递两个指针,我们可以把指针放到结构体中,这样在插入第一个时也可以解决要传递二级指针的问题。

    定义尾指针可以避免每次尾插时要遍历一遍链表。

    1. typedef int QDateType;
    2. typedef struct QueueNode
    3. {
    4. QDateType val;
    5. struct QueueType* next;
    6. }QNode;
    7. typedef struct Queue
    8. {
    9. QNode* phead;
    10. QNode* ptail;
    11. int size;
    12. }Queue;
    2.2 队列的初始化

    这里的 size 用来记录队列中数据的个数。

    1. void QueueInit(Queue* pq)
    2. {
    3. assert(pq);
    4. pq->phead = pq->ptail = NULL;
    5. pq->size = 0;
    6. }
    2.3 入队

    入队相当于尾插,在入队时我们要考虑链表是否为空。

    1. void QueuePush(Queue* pq, QDateType x)
    2. {
    3. assert(pq);
    4. QNode* newnode = (QNode*)malloc(sizeof(QNode));
    5. if (newnode == NULL)
    6. {
    7. perror("malloc fail");
    8. return;
    9. }
    10. newnode->next = NULL;
    11. newnode->val = x;
    12. if (pq->ptail == NULL)
    13. {
    14. pq->phead = pq->ptail = newnode;
    15. }
    16. else
    17. {
    18. pq->ptail->next = newnode;
    19. pq->ptail = newnode;
    20. }
    21. pq->size++;
    22. }
    2.4 出队

    出队相当于头删,与之前不同的是,当我们删除最后一个节点,还要记得处理尾指针。

    1. void QueuePop(Queue* pq)
    2. {
    3. assert(pq);
    4. assert(pq->phead);
    5. QNode* del = pq->phead;
    6. pq->phead = pq->phead->next;
    7. free(del);
    8. del = NULL;
    9. if (pq->phead == NULL)
    10. {
    11. pq->ptail = NULL;
    12. }
    13. pq->size--;
    14. }
    2.5 获取队头元素

    队头元素就是头指针指向的节点的数据域。

    1. QDateType QueueFront(Queue* pq)
    2. {
    3. assert(pq);
    4. assert(pq->phead);
    5. return pq->phead->val;
    6. }
    2.6 获取队尾元素

    我们通过尾指针可以直接找到队尾,不用遍历链表。

    1. QDateType QueueBack(Queue* pq)
    2. {
    3. assert(pq);
    4. assert(pq->phead);
    5. return pq->ptail->val;
    6. }
    2.7 判断空队列

    利用bool的函数判断队列是否为空,当尾指针为空时,返回true;当尾指针不为空时,返回false。

    1. bool QueueEmpty(Queue* pq)
    2. {
    3. assert(pq);
    4. return pq->phead == NULL;
    5. }
    2.8 队列的销毁
    1. int QueueSize(Queue* pq)
    2. {
    3. assert(pq);
    4. return pq->size;
    5. }

    3. 队列完整源码

    Queue.h
    1. #include
    2. #include
    3. #include
    4. #include
    5. typedef int QDateType;
    6. typedef struct QueueNode
    7. {
    8. QDateType val;
    9. struct QueueType* next;
    10. }QNode;
    11. typedef struct Queue
    12. {
    13. QNode* phead;
    14. QNode* ptail;
    15. int size;
    16. }Queue;
    17. void QueueInit(Queue* pq);
    18. void QueueDstroy(Queue* pq);
    19. void QueuePush(Queue* pq, QDateType x);
    20. void QueuePop(Queue* pq);
    21. QDateType QueueFront(Queue* pq);
    22. QDateType QueueBack(Queue* pq);
    23. bool QueueEmpty(Queue* pq);
    24. int QueueSize(Queue* pq);
    Queue.c
    1. #include"Queue.h"
    2. void QueueInit(Queue* pq)
    3. {
    4. assert(pq);
    5. pq->phead = pq->ptail = NULL;
    6. pq->size = 0;
    7. }
    8. void QueueDstroy(Queue* pq)
    9. {
    10. assert(pq);
    11. QNode* cur = pq->phead;
    12. while (cur)
    13. {
    14. QNode* next = cur->next;
    15. free(cur);
    16. cur = next;
    17. }
    18. pq->phead = pq->ptail = NULL;
    19. pq->size = 0;
    20. }
    21. void QueuePush(Queue* pq, QDateType x)
    22. {
    23. assert(pq);
    24. QNode* newnode = (QNode*)malloc(sizeof(QNode));
    25. if (newnode == NULL)
    26. {
    27. perror("malloc fail");
    28. return;
    29. }
    30. newnode->next = NULL;
    31. newnode->val = x;
    32. if (pq->ptail == NULL)
    33. {
    34. pq->phead = pq->ptail = newnode;
    35. }
    36. else
    37. {
    38. pq->ptail->next = newnode;
    39. pq->ptail = newnode;
    40. }
    41. pq->size++;
    42. }
    43. void QueuePop(Queue* pq)
    44. {
    45. assert(pq);
    46. assert(pq->phead);
    47. QNode* del = pq->phead;
    48. pq->phead = pq->phead->next;
    49. free(del);
    50. del = NULL;
    51. if (pq->phead == NULL)
    52. {
    53. pq->ptail = NULL;
    54. }
    55. pq->size--;
    56. }
    57. QDateType QueueFront(Queue* pq)
    58. {
    59. assert(pq);
    60. assert(pq->phead);
    61. return pq->phead->val;
    62. }
    63. QDateType QueueBack(Queue* pq)
    64. {
    65. assert(pq);
    66. assert(pq->phead);
    67. return pq->ptail->val;
    68. }
    69. bool QueueEmpty(Queue* pq)
    70. {
    71. assert(pq);
    72. return pq->phead == NULL;
    73. }
    74. int QueueSize(Queue* pq)
    75. {
    76. assert(pq);
    77. return pq->size;
    78. }

    本次的内容到这里就结束啦。希望大家阅读完可以有所收获,同时也感谢各位读者三连支持。文章有问题可以在评论区留言,博主一定认真认真修改,以后写出更好的文章。你们的支持就是博主最大的动力。

  • 相关阅读:
    【云原生之Docker实战】使用Docker部署Pichome个人相册系统
    一款神器的 Python 工具,不写一行代码,就可以调用 Matplotlib 绘图
    如何快速安装MONAI(莫奈)医学标注软件
    【Python】os包的使用教程详解
    Android平台GB28181设备接入模块之SmartGBD
    根据消息信息如何快速定位程序
    Keras实现——预训练卷积神经网络(VGG16)
    springboot+redis+阿里云短信实现手机号登录
    阿里面试官终于把多年总结的Java八股文PDF版分享出来了,帮我金九银十拿下4个offer
    抖音视频评论采集软件|抖音数据抓取工具
  • 原文地址:https://blog.csdn.net/m0_68662723/article/details/134490576