找回密码
 立即注册
首页 业界区 安全 线性结构常见应用之队列[基于郝斌课程] ...

线性结构常见应用之队列[基于郝斌课程]

跑两獗 2025-9-25 20:58:02
定义:
  一种可以实现“先进先出”的存储结构
  队列类似于排队买票
分类:
  链式队列:基于列表
  静态队列:基于数组
    静态队列通常都必须是循环队列
      静态队列为什么是循环队列?
        减少对内存的浪费
        用传统数组来实现队列的话,参数只能加不能减
      循环队列需要几个参数来确定以及各个参数的含义
        需要两个参数来确定:front 和 rear
        两个参数不同场合有不同的含义
          队列初始化: front 和 rear 都是0
          队列非空:front 代表的是队列的第一个元素, rear 代表的是队列的最后一个有效元素的下一个元素
          队列空:front 和 rear 是相等的,但是不一致是0
      循环队列的入队伪算法
        rear = (rear + 1) % 数组的长度
      循环队列的出队伪算法
        front = (front + 1) % 数组的长度
      如何判断循环队列是否为空?
        如果 front == rear,则队列为空
      如何判断循环队列是否已满?
       front 和 rear 大小没有规律,可以 front 比 rear 大,也可以是 front 比 rear 小 
       规定放入的元素比数组长度少1,只要 rear 和 front 相邻,则满了
       如果 ((rear + 1) % 数组长度) == front,则队列满了
队列的应用:
  所有和时间有关的操作都与队列有关
  1. /*
  2. @file      main.c
  3. @brief     线性结构常见应用之队列
  4. @author    EricsT (EricsT@163.com)
  5. @version   v1.0.0
  6. @date      2025-09-24
  7. @history   2025-09-24 EricsT - 新建文件
  8. */
  9. #include <stdio.h>
  10. #include <malloc.h>
  11. #define LEN 6
  12. typedef struct QUEUE
  13. {
  14.         int* pBase;
  15.         int front;
  16.         int rear;
  17. }Queue, *PtrQueue;
  18. void InitQueue(PtrQueue ptrQueue);//初始化
  19. bool InQueue(PtrQueue ptrQueue, int inValue);//入队
  20. bool OutQueue(PtrQueue ptrQueue);//出队
  21. bool isEmptyQueue(const PtrQueue ptrQueue);//判断是否为空
  22. bool isFullQueue(const PtrQueue ptrQueue);//判断是否满了
  23. void TraverseQueue(const PtrQueue ptrQueue);//遍历
  24. int main(void)
  25. {
  26.         Queue queue;
  27.         InitQueue(&queue);
  28.         if (isEmptyQueue(&queue))
  29.                 printf("队列为空\n");
  30.         else
  31.                 printf("队列不空\n");
  32.         int inVaule;
  33.         printf("请输入要入队的值:");
  34.         scanf("%d", &inVaule);
  35.         InQueue(&queue, inVaule);
  36.         printf("请输入要入队的值:");
  37.         scanf("%d", &inVaule);
  38.         InQueue(&queue, inVaule);
  39.         printf("请输入要入队的值:");
  40.         scanf("%d", &inVaule);
  41.         InQueue(&queue, inVaule);
  42.         printf("请输入要入队的值:");
  43.         scanf("%d", &inVaule);
  44.         InQueue(&queue, inVaule);
  45.         printf("请输入要入队的值:");
  46.         scanf("%d", &inVaule);
  47.         InQueue(&queue, inVaule);
  48.         printf("请输入要入队的值:");
  49.         scanf("%d", &inVaule);
  50.         InQueue(&queue, inVaule);
  51.         printf("请输入要入队的值:");
  52.         scanf("%d", &inVaule);
  53.         InQueue(&queue, inVaule);
  54.         if (isEmptyQueue(&queue))
  55.                 printf("队列为空\n");
  56.         else
  57.                 printf("队列不空\n");
  58.         if (isFullQueue(&queue))
  59.                 printf("队列已满\n");
  60.         else
  61.                 printf("队列未满\n");
  62.         TraverseQueue(&queue);
  63.         OutQueue(&queue);
  64.         TraverseQueue(&queue);
  65.         return 0;
  66. }
  67. void InitQueue(PtrQueue ptrQueue)
  68. {
  69.         ptrQueue->pBase = (int*)malloc(sizeof(int) * LEN);
  70.         ptrQueue->front = 0;
  71.         ptrQueue->rear = 0;
  72. }
  73. bool InQueue(PtrQueue ptrQueue, int inValue)
  74. {
  75.         if (isFullQueue(ptrQueue))
  76.                 return false;
  77.         ptrQueue->pBase[ptrQueue->rear] = inValue;
  78.         ptrQueue->rear = (ptrQueue->rear + 1) % LEN;//从后面加入
  79.         return true;
  80. }
  81. bool OutQueue(PtrQueue ptrQueue)
  82. {
  83.         if (isEmptyQueue(ptrQueue))
  84.                 return false;
  85.         ptrQueue->front = (ptrQueue->front + 1) % LEN;//先进先出//从前面出去
  86.         return true;
  87. }
  88. bool isEmptyQueue(const PtrQueue ptrQueue)
  89. {
  90.         if (ptrQueue->front == ptrQueue->rear)
  91.                 return true;
  92.         return false;
  93. }
  94. bool isFullQueue(const PtrQueue ptrQueue)
  95. {
  96.         if (ptrQueue->front == ((ptrQueue->rear + 1) % LEN))
  97.                 return true;
  98.         return false;
  99. }
  100. void TraverseQueue(const PtrQueue ptrQueue)
  101. {
  102.         int i = ptrQueue->front;
  103.         while (ptrQueue->rear != i)
  104.         {
  105.                 printf("%d ", ptrQueue->pBase[i]);
  106.                 i = (i + 1) % LEN;
  107.         }
  108.         printf("\n");
  109. }
复制代码
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册