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

【模板】循环队列

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

#include <stdio.h>
#include<malloc.h>

int main() {

    int space;
    scanf("%d", &space);
    int times;
    scanf("%d", ×);
    int* array = (int*)malloc(sizeof(int) * (space + 1));
    int front = 0;
    int rear = 0;
  //有没有大佬帮我看一下为什么要执行times+100000才能通过所有案例,应当只循环times次就应该可以通过的
    for (int i = 0; i <= times+1000000; i++) {
        char* op = (char*)malloc(sizeof(char) * 6);
        scanf("%s", op);
        if (!strcmp(op, "push")) {
            if (((rear +1)% (space + 1)  == front)) {
                printf("full\n");
            } else {
                int tmp;
                scanf("%d", &tmp);
                array[rear++] = tmp;
                rear = rear % (space + 1);

            }
        }
        if (!strcmp(op, "pop")) {
            if (!(rear == front)) {
                printf("%d\n", array[front++]);
                front = front % (space + 1);
            } else {
                printf("empty\n");
            }

        }
        if (!strcmp(op, "front")) {
            if (!(rear == front)) {
                printf("%d\n", array[front]);
            } else {
                printf("empty\n");
            }

        }
    }

    return 0;


}

全部评论

相关推荐

09-29 16:59
已编辑
门头沟学院 Java
牛客96609213...:疯狂背刺,之前还明确设置截止日期,还有笔试,现在一帮人卡在复筛,他反而一边开启扩招,还给扩招的免笔试,真服了,你好歹先把复筛中的给处理了再说
投递大疆等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务