• 秩序的美——队列的基础实现


    目录

    一、基础设置

    二、函数实现

    1. 初始化

    2. 入队出队

    3. 获取队头队尾

    4. 判断队空

    5. 队列大小

    三、代码汇总

    Queue.h

    Queue.c


    队列:先进先出

    一、基础设置

    此处用链表来实现队列

    第一个结构体定义的是结点(类似链表),第二个定义的是队列(包含头尾指针和大小)

    1. typedef int QType;
    2. typedef struct QueueNode
    3. {
    4. struct Queue* next;
    5. QType data;
    6. }QNode;
    7. typedef struct Queue
    8. {
    9. QNode* tail;
    10. QNode* head;
    11. int size;
    12. }Queue;

    二、函数实现

    1. 初始化

    1. void QInit(Queue* pq)
    2. {
    3. assert(pq);
    4. pq->head = NULL;
    5. pq->tail = NULL;
    6. pq->size = 0;
    7. }

    2. 入队出队

    思路与前面的入栈出栈类似,不同的是栈是后进先出,队列是先进先出

    1. void QPush(Queue* pq, QType x)
    2. {
    3. assert(pq);
    4. QNode* tmp = (QNode*)malloc(sizeof(QNode));
    5. if (tmp == NULL)
    6. {
    7. perror("malloc");
    8. exit(-1);
    9. }
    10. tmp->data = x;
    11. tmp->next = NULL;
    12. if (pq->tail == NULL)
    13. {
    14. pq->head = pq->tail = tmp;
    15. }
    16. else
    17. {
    18. pq->tail->next = tmp;
    19. pq->tail = tmp;
    20. }
    21. pq->size++;
    22. }
    1. void QPop(Queue* pq)
    2. {
    3. assert(pq);
    4. assert(pq->size > 0);
    5. QNode* next = pq->head->next;
    6. QNode* cur = pq->head;
    7. free(cur);
    8. cur = NULL;
    9. pq->head = next;
    10. if (pq->head == NULL)
    11. pq->tail = NULL;
    12. pq->size--;
    13. }

    3. 获取队头队尾

    因为我们定义了头尾指针,这个就很好操作了

    1. QType QHead(Queue* pq)
    2. {
    3. assert(pq);
    4. assert(pq->head);
    5. return pq->head->data;
    6. }
    1. QType QTail(Queue* pq)
    2. {
    3. assert(pq);
    4. assert(pq->tail);
    5. return pq->tail->data;
    6. }

     

    4. 判断队空

    1. bool QEmpty(Queue* pq)
    2. {
    3. assert(pq);
    4. return pq->size == 0;
    5. }

    5. 队列大小

    1. int QSize(Queue* pq)
    2. {
    3. assert(pq);
    4. return pq->size;
    5. }

    三、代码汇总

    Queue.h

    1. #include
    2. #include
    3. #include
    4. #include
    5. typedef int QType;
    6. typedef struct QueueNode
    7. {
    8. struct Queue* next;
    9. QType data;
    10. }QNode;
    11. typedef struct Queue
    12. {
    13. QNode* tail;
    14. QNode* head;
    15. int size;
    16. }Queue;
    17. void QInit(Queue* pq);
    18. void QDestroy(Queue* pq);
    19. void QPush(Queue* pq, QType x);
    20. void QPop(Queue* pq);
    21. QType QHead(Queue* pq);
    22. QType QTail(Queue* pq);
    23. bool QEmpty(Queue* pq);
    24. int QSize(Queue* pq);

    Queue.c

    1. #include "Queue.h"
    2. void QInit(Queue* pq)
    3. {
    4. assert(pq);
    5. pq->head = NULL;
    6. pq->tail = NULL;
    7. pq->size = 0;
    8. }
    9. void QDestroy(Queue* pq)
    10. {
    11. assert(pq);
    12. QNode* cur = pq->head;
    13. while (cur != NULL)
    14. {
    15. QNode* next = cur->next;
    16. free(cur);
    17. cur = next;
    18. }
    19. pq->size = 0;
    20. }
    21. void QPush(Queue* pq, QType x)
    22. {
    23. assert(pq);
    24. QNode* tmp = (QNode*)malloc(sizeof(QNode));
    25. if (tmp == NULL)
    26. {
    27. perror("malloc");
    28. exit(-1);
    29. }
    30. tmp->data = x;
    31. tmp->next = NULL;
    32. if (pq->tail == NULL)
    33. {
    34. pq->head = pq->tail = tmp;
    35. }
    36. else
    37. {
    38. pq->tail->next = tmp;
    39. pq->tail = tmp;
    40. }
    41. pq->size++;
    42. }
    43. void QPop(Queue* pq)
    44. {
    45. assert(pq);
    46. assert(pq->size > 0);
    47. QNode* next = pq->head->next;
    48. QNode* cur = pq->head;
    49. free(cur);
    50. cur = NULL;
    51. pq->head = next;
    52. if (pq->head == NULL)
    53. pq->tail = NULL;
    54. pq->size--;
    55. }
    56. QType QHead(Queue* pq)
    57. {
    58. assert(pq);
    59. assert(pq->head);
    60. return pq->head->data;
    61. }
    62. QType QTail(Queue* pq)
    63. {
    64. assert(pq);
    65. assert(pq->tail);
    66. return pq->tail->data;
    67. }
    68. bool QEmpty(Queue* pq)
    69. {
    70. assert(pq);
    71. return pq->size == 0;
    72. }
    73. int QSize(Queue* pq)
    74. {
    75. assert(pq);
    76. return pq->size;
    77. }

    如果有不懂的部分可以在评论区一起讨论,如果c语言的知识有要回顾的可以看我之前写过的文章

  • 相关阅读:
    视频共享融合赋能平台LntonCVS统一视频接入平台数字化升级医疗体系
    【会话技术基础】
    【强化学习论文合集】ICLR-2021 强化学习论文
    ROS create_wall_timer/create_timer函数区别
    C++ To Infinity | AtCoder
    Linux 两台服务器之间传输文件和文件夹的方法
    103.(cesium之家)cesium蜂巢图(正方形)
    组件通信-父传子组件通信
    【SpringBoot+Vue】前后端分离项目之图片上传与下载
    小波去噪算法的简易实现及其扩展(小波锐化、高斯拉普拉斯金字塔去噪及锐化)之一。
  • 原文地址:https://blog.csdn.net/m0_73969414/article/details/134534576