题解 | 【模板】循环队列

【模板】循环队列

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

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

int main() {
    int n, q;
    scanf("%d", &n);
    scanf("%d", &q);
    char s[6];
    int x;
    int rear = 0;
    int top = 0;
    //int que[n];
    int *queue=(int *)malloc((n+1)*sizeof(int));
    int i = 0;
        while (i < q) {
            scanf("%s", s);
            if (s[0] == 'p' && s[1] == 'u') {
                scanf("%d", &x);
                if ((rear+1)%(n+1)==top) {
                    printf("full\n");
                    i++;
                    continue;
                }               
                queue[rear] = x;
                rear = (rear + 1) % (n+1);              
            }
            if (s[0] == 'p' && s[1] == 'o') {
                if (top == rear ) {
                    printf("empty\n");
                } else {
                    printf("%d\n", queue[top]);                  
                    top = (top + 1) % (n+1);
                }
            }
            if (s[0] == 'f') {
                if (top == rear ) {
                    printf("empty\n");
                } else {
                    printf("%d\n", queue[top]);
                }
            }
            i++;
        }   
    free(queue);
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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