zoukankan      html  css  js  c++  java
  • 05_队列

    一、什么是队列

    一种可以实现“先进先出”的数据结构,

    队列的分类:

    1.静态队列:数组实现

    2.链式队列:链表实现

    二、队列的具体应用

    一切和时间有关的操作都与队列有关,重注优先级。

    三、静态队列

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <malloc.h>
     4 
     5 //循环队列的组织方式 
     6 typedef struct Queue
     7 {
     8     int *pBase;
     9     int front;
    10     int rear;
    11 }QUEUE,* PQUEUE;
    12 
    13 void init(PQUEUE);
    14 void insert_queue(PQUEUE,int val);
    15 void treverse_queue(PQUEUE);
    16 bool full_queue(PQUEUE);
    17 bool out_queue(PQUEUE);
    18 bool empty_queue(PQUEUE);
    19 
    20 int main(void)
    21 {
    22     Queue queue;
    23     init(&queue);
    24     insert_queue(&queue,1);
    25     insert_queue(&queue,2);
    26     insert_queue(&queue,3);
    27     insert_queue(&queue,4);
    28     insert_queue(&queue,5);
    29     insert_queue(&queue,6);
    30     insert_queue(&queue,7);
    31     insert_queue(&queue,8);
    32     treverse_queue(&queue);
    33     out_queue(&queue);
    34     treverse_queue(&queue);
    35 }
    36 
    37 void init(PQUEUE pQ)
    38 {
    39     pQ->pBase = (int*) malloc (sizeof(int) * 6);
    40     pQ->front = 0;
    41     pQ->rear = 0;
    42 }
    43 
    44 bool full_queue(PQUEUE pQ)
    45 {
    46     if((pQ->rear + 1) %6 == pQ->front){
    47         return true;
    48     }
    49     return false;
    50 }
    51 
    52 void insert_queue(PQUEUE pQ,int val)
    53 {
    54     if(full_queue(pQ))
    55     {
    56         printf("队列已满,插入失败!
    ") ;
    57         return ;
    58     }
    59     pQ->pBase[pQ->rear] = val;
    60     pQ->rear = (pQ->rear + 1) % 6;
    61 }
    62 
    63 void treverse_queue(PQUEUE pQ)
    64 {
    65     if(empty_queue (pQ)) {
    66         printf("链表为空!
    ");
    67         return; 
    68     }
    69     int i = pQ->front;
    70     while(i != pQ->rear)
    71     {
    72         printf("队列数据 : %d 
    ",pQ->pBase[i]);
    73         i = (i+1) %6;
    74     }
    75     return;
    76 }
    77 
    78 bool empty_queue(PQUEUE pQ)
    79 {
    80     if(pQ->front == pQ->rear){
    81         return true;
    82     }
    83     else
    84         return false;
    85 }
    86 
    87 
    88 bool out_queue(PQUEUE pQ)
    89 {
    90     
    91     while(pQ->front != pQ->rear)
    92     {
    93         pQ->front = (pQ->front + 1) % 6;
    94     }
    95 }
  • 相关阅读:
    [知乎]20世纪初的军阀.
    Clover的简单使用
    影像工作站的数据库安装错误之Win7系统下pg服务无法启动
    屏蔽各大视频网站播放前15秒30秒广告
    电脑双显示器主分屏,巨鲨显示器不亮
    move 和 CopyMemory的区别
    The CompilerVersion constant identifies the internal version number of the Delphi compiler.
    Firemonkey的旁门左道[六]
    电够动力足——认识主板上的CPU供电模块
    delphi 枚举类型
  • 原文地址:https://www.cnblogs.com/weihengblog/p/8410008.html
Copyright © 2011-2022 走看看