• 【数据结构】——顺序循环队列的相关操作


      队列是一种先进先出的数据存储结构,一般操作系统中用的比较多,本文主要介绍对顺序队列的入队列,出队列,遍历队列操作。

      定义顺序队列:

      我们可以用一个数组来表示一个顺序存储结构,两个整数来分别指向数组的下标,表示队列的头指针和尾指针;

    typedef struct queue
    {
        int data[MAX];
        int front;      //头指针
        int rear;       //尾指针
    }queue;

      定义队列之后首先就是初始化队列:

      初始化队列的时候队列一定是空的,队列的头指针和尾指针必须指向数组的首端;

    queue *init()
    {
        queue *h;
        h = (queue *)malloc(sizeof(queue));
        h->front = 0;
        h->rear = 0;
        return h;    
    }

      定义队列的存储结构并且初始化队列之后,接下来就要入队列了。

      在入队列的时候需要注意几个问题;

      ①:入队列是必须判断队列是否已满,可以用一个数学公式来判断:(h->rear + 1) % MAX == h->front 如果该表达式为TRUE,说明队列已满;

      ②:把此队列设置成循环队列,即当h->rear走到数组末端的时候,必须考虑把h->rear指向数组的首端(0);【入队列移动的是队列的尾指针】

      ③:连续入队列的时候必须在入队列之后检查队列是否已满;

      我们有了这些规则之后就可以进行编码了:

    void insert(queue *h)        //插入队列
    {
        int value;
        char ch;
        if((h->rear + 1) % MAX == h->front)                //判断队列是否已满
            printf("队列已满,请删除后再插入!\n");
        else
        {
            do
            {
                printf("请输入需要插入的值:");
                value = get_int();
    
                h->data[h->rear] = value;
                h->rear++;
    
                if(h->rear == MAX)                //因为是循环队列,所以当队列不满时,并且h->rear已大于数组下标时,h->rear要指向数组初始位置
                    h->rear = 0;
    
                if((h->rear + 1) % MAX == h->front)            //入队列之后要进一步的判断队列是否已满
                {
                    printf("队列已满,请删除后再插入!\n");
                    ch = 'n';
                }
                else
                {
                    do
                    {
                        printf("是否需要继续插入?y/n");
                        ch = get_first();
                    }while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N');
                }
            }while(ch == 'y' || ch == 'Y');
        }
    
    }

      出队列需要注意的问题:

      ①:判断队列是否为空;我们认为当头指针等于尾指针是队列为空;

      ②:当队列的头指针走到数组尾部是需要改变头指针指向数组头部;【出队列只改变头指针的位置】

      ③:连续出队列的时候必须在出队列之后检查队列是否为空;

    void del(queue *h)        //出队列
    {
        int value;
        char ch;
        if(h->front == h->rear)
            printf("队列以空!!!\n");
        else
        {
            do
            {
                value = h->data[h->front];
                h->data[h->front] = 0;
                h->front++;
                
                printf("\n%d 出队列\n",value);
    
                if(h->front == MAX)
                    h->front = 0;
    
                if(h->front == h->rear)
                {
                    printf("队列以空!!!\n");
                    ch = 'n';
                }
                else
                {
                    do
                    {
                        printf("是否需要继续出队列?y/n\n");
                        ch = get_first();
                    }while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N');
                }
            }while(ch == 'y' || ch == 'Y');
        }
    }

      遍历队列:

    void bianli(queue *h)        //遍历队列中的元素
    {
        int i,j;
        if(h->front == h->rear)
            printf("队列为空!!!\n");
        else
        {
            j = h->front;
            printf("出队列的顺序为:\n");
            for(i = 0;i < (h->rear - h->front + MAX) % MAX;i++)
            {
                printf("the %d value is %d\n",i + 1,h->data[j]);
                j++;
            }    
        }
    }

       完整的代码如下:【代码粘不上了,只能这样了】

    完整代码
      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 
      4 #define MAX 5
      5 
      6 typedef struct queue
      7 {
      8     int data[MAX];
      9     int front;
     10     int rear;
     11 }queue;
     12 
     13 queue *h;    //队列头结点
     14 
     15 
     16 queue *init()
     17 {
     18     queue *h;
     19     h = (queue *)malloc(sizeof(queue));
     20     h->front = 0;
     21     h->rear = 0;
     22     return h;    
     23 }
     24 
     25 
     26 
     27 int get_int()            //得到整型
     28 {
     29     int input;
     30     char ch;
     31     while(scanf("%d",&input) != 1)
     32     {
     33         while((ch = getchar()) != '\n');        //截取多余的字符串
     34         printf("输入格式不对,请重新输入!\n");
     35     }
     36     while((ch = getchar()) != '\n');        //截取多余的字符串
     37     return input;
     38     
     39 }
     40 
     41 char get_first()    //得到第一个字符
     42 {
     43     char c;
     44     scanf("%c",&c);
     45     while(getchar() != '\n')
     46         continue;
     47     return c;
     48 }
     49 
     50 int menu()        //选择菜单
     51 {
     52     int result;
     53     printf("**********请选择:***********\n");
     54     printf("**********1.插入:***********\n");
     55     printf("**********2.删除:***********\n");
     56     printf("**********3.遍历:***********\n");
     57 
     58     result = get_int();    
     59     while(result > 3 || result < 1)
     60     {
     61         printf("请输入1-3!\n");
     62         result = get_int();
     63     }
     64         return result;
     65 }
     66 
     67 void insert(queue *h)        //插入队列
     68 {
     69     int value;
     70     char ch;
     71     if((h->rear + 1) % MAX == h->front)                //判断队列是否已满
     72         printf("队列已满,请删除后再插入!\n");
     73     else
     74     {
     75         do
     76         {
     77             printf("请输入需要插入的值:");
     78             value = get_int();
     79 
     80             h->data[h->rear] = value;
     81             h->rear++;
     82 
     83             if(h->rear == MAX)                //因为是循环队列,所以当队列不满时,并且h->rear已大于数组下标时,h->rear要指向数组初始位置
     84                 h->rear = 0;
     85 
     86             if((h->rear + 1) % MAX == h->front)            //入队列之后要进一步的判断队列是否已满
     87             {
     88                 printf("队列已满,请删除后再插入!\n");
     89                 ch = 'n';
     90             }
     91             else
     92             {
     93                 do
     94                 {
     95                     printf("是否需要继续插入?y/n");
     96                     ch = get_first();
     97                 }while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N');
     98             }
     99 
    100         }while(ch == 'y' || ch == 'Y');
    101     }
    102 
    103 }
    104 
    105 void bianli(queue *h)        //遍历队列中的元素
    106 {
    107     int i,j;
    108     if(h->front == h->rear)
    109         printf("队列为空!!!\n");
    110     else
    111     {
    112         j = h->front;
    113         printf("出队列的顺序为:\n");
    114         for(i = 0;i < (h->rear - h->front + MAX) % MAX;i++)
    115         {
    116             printf("the %d value is %d\n",i + 1,h->data[j]);
    117             j++;
    118         }    
    119     }
    120 }
    121 
    122 void del(queue *h)        //出队列
    123 {
    124     int value;
    125     char ch;
    126     if(h->front == h->rear)
    127         printf("队列以空!!!\n");
    128     else
    129     {
    130         do
    131         {
    132             value = h->data[h->front];
    133             h->data[h->front] = 0;
    134             h->front++;
    135             
    136             printf("\n%d 出队列\n",value);
    137 
    138             if(h->front == MAX)
    139                 h->front = 0;
    140 
    141             if(h->front == h->rear)
    142             {
    143                 printf("队列以空!!!\n");
    144                 ch = 'n';
    145             }
    146             else
    147             {
    148                 do
    149                 {
    150                     printf("是否需要继续出队列?y/n\n");
    151                     ch = get_first();
    152                 }while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N');
    153             }
    154         }while(ch == 'y' || ch == 'Y');
    155     }
    156 }
    157 
    158 int main(void)
    159 {
    160     char ch;
    161     h = init();
    162     do
    163     {
    164         switch(menu())
    165         {
    166             case 1: insert(h);break;
    167             case 2: del(h);break;
    168             case 3: bianli(h);break;
    169         }
    170         do
    171         {
    172             printf("是否要继续操作?(y/n)");
    173             ch = get_first();
    174         }while(ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N');
    175     }while(ch == 'y' || ch == 'Y');
    176     
    177 }
  • 相关阅读:
    CentOS 8上安装MySQL 8.0
    centos8安装redis
    virtualbox虚拟机ubuntu18.04网络配置--虚拟机和宿主机互通且联网
    Controller同时接收MultipartFile数据和json数据
    日期转换
    Python的Mysql操作
    Go语言学习笔记十三: Map集合
    Go语言学习笔记十二: 范围(Range)
    Go语言学习笔记十一: 切片(slice)
    Go语言学习笔记十: 结构体
  • 原文地址:https://www.cnblogs.com/ngnetboy/p/2966107.html
Copyright © 2020-2023  润新知