【牛客刷题专栏】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

解法思路:

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

代码结果:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

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;
}

重点难点:

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


结束语

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

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不僈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值