题解 | #【模板】循环队列#

【模板】循环队列

https://www.nowcoder.com/practice/0a3a216e50004d8bb5da43ad38bcfcbf

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

int arr[100000],sum=0,first=0,mx;
void push(int x)
{
    if(sum+1-first>mx) printf("full\n");
    else arr[sum++]=x;
}
void front()
{
    if(sum==first) printf("empty\n");
    else printf("%d\n",arr[first]);
}
void pop()
{
    if(sum==first) printf("empty\n");
    else printf("%d\n",arr[first++]);
}
int main() 
{
    int n;
    scanf("%d%d",&mx,&n);
    getchar();
    while(n--)
    {
        int x=0;
        char str[10];
        int number=0;
        scanf("%s", str);       
        if (strcmp(str, "push") == 0) scanf("%d", &x);      
        if (strcmp(str, "front") == 0) front();
        else if (strcmp(str, "pop") == 0) pop();
        else if (strcmp(str, "push") == 0) push(x);      
        getchar();
    }
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务