• 【牛客刷题专栏】0x06:C数据结构栈实现循环队列及其重点


    前言


    问题描述:

    请你实现一个循环队列,该循环队列可利用的空间大小等于nn个int型变量的大小。
    操作:
    push x:将xx加入到循环队列尾端。若循环队列已满,输出"full"(不含引号),否则不输出任何内容。保证xx为int型整数。
    front:输出队首元素,队首不出队。若队列为空,输出"empty"(不含引号)。
    pop:输出队首元素,且队首出队。若队列为空,输出"empty"(不含引号)。


    输入描述:

    第一行输入两个整数n,q (1≤n,q≤10^5 ),表示循环队列可利用的空间大小和操作次数。
    接下来的qq行,每行一个字符串,表示一个操作。保证操作是题目描述中的一种。


    输出描述:

    按对应操作要求输出。


    举例:

    //输入:
    3 10
    push 1
    push 2
    front
    push 3
    push 4
    pop
    pop
    pop
    front
    pop
    //输出:
    1
    full
    1
    2
    3
    empty
    empty
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    解法思路:

    • 【C语言数据结构】06.循环队列
    • 此题基本和在 C语言数据结构专题中的循环队列一致,只不过增加了push等命令识别,以及队列大小确定的需求,参考答题区代码最终答案如下。

    代码结果:

    #include 
    #include 
    #include 
    #include 
    
    typedef struct
    {
        int* data;
        int front;
        int rear;
        int capacity;//空间容量 = 固定长度 + 1 = n + 1,多开一个方便判空、判满
    }cirQueue;
    
    void QueueInit(cirQueue* obj,int n)//初始化头节点
    {
        obj->data = (int*)malloc(sizeof(int) * (n + 1));
        //因为如果存满n+1个数,而faont与rear相等时,无法区分队列是满还是空
        //因此使用牺牲rear指向的一个元素,如n+1=5,则只存4个,即n个:当faont与rear+1相等时即认为满,不再往rear里写入;faont与rear相等时为空
        obj->front = obj->rear = 0;
        obj->capacity = n + 1;
    }
    
    bool QueueIsFull(cirQueue* obj)//检查队列是否为满
    {
        //rear
        return (obj->rear + 1) % obj->capacity == obj->front;
    }
    
    bool QueueIsEmpty(cirQueue* obj)//检查队列是否为空
    {
        return obj->front == obj->rear;
    }
    
    bool QueuePush(cirQueue* obj,int x)//入队
    {
        if (QueueIsFull(obj))
        {
            return false;
        }
        obj->data[obj->rear++] = x;
        obj->rear %= obj->capacity;
        return true;
    }
    
    //empty传址用来判断循环队列是不是空
    int QueueFront(cirQueue* obj,int* empty)//输出队首元素,队首不出队
    {
        if (QueueIsEmpty(obj))
        {
            *empty = 1;
            return 0;
        }
        return obj->data[obj->front];
    }
    
    int QueuePop(cirQueue* obj,int* empty)//输出队首元素,且队首出队
    {
        if (QueueIsEmpty(obj))
        {
            *empty = 1;
            return 0;
        }
        int front = obj->data[obj->front++];
        obj->front %= obj->capacity;
        return front;
    }
    
    int main()
    {
        int n = 0,q = 0;
        cirQueue obj;
        scanf("%d%d",&n,&q);
        QueueInit(&obj,n);
        while (q--)
        {
            int empty = 0;
            char s[6] = { 0 };
            scanf("%s",&s);
            if (strcmp(s,"push") == 0)
            {
                int x = 0;
                scanf("%d",&x);
                bool flag = QueuePush(&obj,x);
                if (!flag)
                {
                    printf("full\n");
                }
    
            }
            else if (strcmp(s,"pop") == 0)
            {
                int front = QueuePop(&obj,&empty);
                if (empty == 1)
                {
                    printf("empty\n");
                }
                else
                {
                    printf("%d\n",front);
                }
            }
            else
            {
                int front = QueueFront(&obj,&empty);
                if (empty == 1)
                {
                     printf("empty\n");
                }
                else
                {
                    printf("%d\n",front);
                }
            }
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    重点难点:

    循环队列的话,后标rear的对应元素始终是空的,因为要区分空和满,前标front和rear相等是空,rear+1取余和front相等是满,这就把后标对应的元素给牺牲了,便不能存了,因此可存数据数量实际大小少1。


    结束语

    • 以上就是C数据结构栈实现循环队列的内容。可以在牛客尝试刷几道数据结构题目来练习实践。牛客网刷题(点击可以跳转),可以尝试注册使用。

    在这里插入图片描述

  • 相关阅读:
    x86-Hardware-Compatibility-Assessment-and-Porting-Guide
    docker 命令
    css定义变量 --**
    预售拼购模式是什么?有什么优势?
    计控实验(二)——积分分离PID控制实验
    高等数学(第七版)同济大学 习题4-5 个人解答
    怎么查看线程的状态及interrupt优雅的关闭线程和interrupt()、interrupted()、isInterrupted()的作用以及区别在哪?
    mysql 自定义函数create function
    家政服务管理系统,家政服务系统平台,家政服务网站毕设作品
    spring boot酒店会员点餐系统毕业设计源码072005
  • 原文地址:https://blog.csdn.net/weixin_43490708/article/details/127416283