• 初阶数据结构学习记录——일곱 队列


    目录

    Queue.h

    Queue.c

    Test.c


    栈是后进先出。队列是先进先出。

    比如push1234四个数字,那么pop时就是先pop1,再pop2。First in first out。

    队列的实现当然也可以使用数组和链表。不过链表实现更方便。因为队列的删除对于链表也就相当于头删,而数组则需要挪动数据。往后插入数据,链表可以使用双指针走即可解决尾插问题。

    选定链表后,我们再想需要什么样的链表,需不需要哨兵位。如果双向链表,优势就是找前一个节点,但是队列貌似不需要这个优势,且论其头尾部操作,双链表不一定比带双指针的单链表简单,这里就敲定单链表。再来想一下哨兵位,其实这个可要可不要,不带头的话我们创建一个结构体,然后在后面插入数据即可,哨兵位带上也可,所以不是必要的东西。

    学习了栈的实现后,队列也会相对简单些。原理都差不多。保持着先进先出的原则,两个指针,一个指向头,一个指向尾。由于要一直使用,所以除了建立一个基本的结构体,我们在建立一个结构体,里面包括head和tail两个结构体指针以及一个size,用来表示放入了几个数据。基本的结构体里还是一个整形,一个next指针,这个整形data用来表示这个节点放入的数据。

    直接放代码

    Queue.h

    1. #pragma once
    2. #define  _CRT_SECURE_NO_WARNINGS 1
    3. #include
    4. #include
    5. #include
    6. #include
    7. typedef int QDataType;
    8. typedef struct QueueNode
    9. {
    10. QDataType data;
    11. struct QueueNode* next;
    12. }QNode;
    13. typedef struct Queue
    14. {
    15. QNode* head;
    16. QNode* tail;
    17. QDataType size;
    18. }Queue;
    19. void QueueInit(Queue* pq);
    20. void QueueDestroy(Queue* pq);
    21. void QueuePush(Queue* pq, QDataType x);
    22. void QueuePop(Queue* pq);
    23. QDataType QueueFront(Queue* pq);
    24. QDataType QueueBack(Queue* pq);
    25. bool QueueEmpty(Queue* pq);
    26. QDataType QueueSize(Queue* pq);
    27. void QueuePrint(Queue* pq);

    Queue.c

    1. bool QueueEmpty(Queue* pq);
    2. void QueueInit(Queue* pq)
    3. {
    4. assert(pq);
    5. pq->head = NULL;
    6. pq->tail = NULL;
    7. pq->size = 0;
    8. }
    9. void QueueDestroy(Queue* pq)
    10. {
    11. assert(pq);
    12. QNode* cur = pq->head;
    13. while (cur)
    14. {
    15. //QNode* next = cur->next;
    16. //free(cur);
    17. //cur = next;
    18. QNode* del = cur;
    19. cur = cur->next;
    20. free(del);
    21. }
    22. pq->head = pq->tail = NULL;
    23. pq->size = 0;
    24. }
    25. void QueuePush(Queue* pq, QDataType x)
    26. {
    27. assert(pq);
    28. QNode* node = (QNode*)malloc(sizeof(QNode));
    29. if (node == NULL)
    30. {
    31. perror("malloc fail");
    32. exit(-1);
    33. }
    34. node->data = x;
    35. node->next = NULL;
    36. if (pq->tail == NULL)
    37. {
    38. pq->head = pq->tail = node;
    39. }
    40. else
    41. {
    42. pq->tail->next = node;
    43. pq->tail = node;
    44. }
    45. pq->size++;
    46. }
    47. void QueuePop(Queue* pq)
    48. {
    49. assert(pq);
    50. assert(!QueueEmpty(pq));
    51. if (pq->head->next == NULL)
    52. {
    53. free(pq->head);
    54. pq->head = pq->tail = NULL;
    55. }
    56. else
    57. {
    58. QNode* node = pq->head;
    59. pq->head = pq->head->next;
    60. free(node);
    61. }
    62. pq->size--;
    63. }
    64. QDataType QueueFront(Queue* pq)
    65. {
    66. assert(pq);
    67. assert(!QueueEmpty(pq));
    68. return pq->head->data;
    69. }
    70. QDataType QueueBack(Queue* pq)
    71. {
    72. assert(pq);
    73. assert(!QueueEmpty(pq));
    74. return pq->tail->data;
    75. }
    76. bool QueueEmpty(Queue* pq)
    77. {
    78. assert(pq);
    79. return pq->head == NULL && pq->tail == NULL;
    80. }
    81. QDataType QueueSize(Queue* pq)
    82. {
    83. assert(pq);
    84. return pq->size;
    85. }



     

    测试代码

    Test.c

    1. void TestQueue()
    2. {
    3. Queue q;
    4. QueueInit(&q);
    5. QueuePush(&q, 1);
    6. QueuePush(&q, 2);
    7. printf("%d ", QueueFront(&q));
    8. QueuePop(&q);
    9. QueuePush(&q, 3);
    10. QueuePush(&q, 4);
    11. printf("%d\n", QueueSize(&q));
    12. printf("%d\n", QueueEmpty(&q));
    13. printf("%d\n", QueueFront(&q));
    14. printf("%d\n", QueueBack(&q));
    15. while (!QueueEmpty(&q))
    16. {
    17. printf("%d ", QueueFront(&q));
    18. QueuePop(&q);
    19. }
    20. printf("\n");
    21. printf("%d\n", QueueSize(&q));
    22. printf("%d\n", QueueEmpty(&q));
    23. //printf("%d\n", QueueFront(&q));
    24. //printf("%d\n", QueueBack(&q));
    25. QueueDestroy(&q);
    26. }
    27. int main()
    28. {
    29. TestQueue();
    30. return 0;
    31. }

    队列不写print函数。

    队列这里就不写太多了

    结束。

  • 相关阅读:
    华为云国际版实名账号:亚太已发展超2500个本地生态伙伴 超50%收入由伙伴创造
    微服务开发工具及环境搭建
    ChatGPT-01 用ChatGPT指令,自学任何领域的系统知识
    亚马逊攀岩绳EN892标准安全测试
    Java面试八股之myBatis动态SQL的作用
    document.activeELement和antdesign-useForm
    力扣+牛客--刷题记录
    vue项目打包成dist文件夹之后后端请求报404错误解决方法
    自动驾驶感知算法实战12——BEV 基于图像/Lidar/多模态数据的3D检测与分割任务
    用WordCloud绘制词云
  • 原文地址:https://blog.csdn.net/kongqizyd146/article/details/127848189