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

【模板】循环队列

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

描述

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

输入描述:

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

输出描述:

按对应操作要求输出。

示例1

输入:
3 10
push 1
push 2
front
push 3
push 4
pop
pop
pop
front
pop
复制
输出:
1
full
1
2
3
empty
empty
解题思路:
简单的队列操作,需要注意的是输入参数只有push指令时才有两个参数。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

int main() {
    int n;
    int q;
    while (cin >> n && cin >> q) {
        queue<int>que;
        for (int i = 0; i < q; i++) {
            string cmd;
            cin >> cmd;
            int num;
            if (cmd == "push") {
                cin >> num;
            }
            
            if (cmd == "push") {
                if (que.size() == n) {
                    cout << "full" << endl;
                } else {
                    que.push(num);
                }
            } else if (cmd == "front") {
                if (que.empty()) {
                    cout << "empty" << endl;
                } else {
                    cout << que.front() << endl;
                }
            } else if (cmd == "pop") {
                if (que.empty()) {
                    cout << "empty" << endl;
                } else {
                    cout << que.front() << endl;
                    que.pop();
                }
            }
        }
        return 0;
    }
}
// 64 位输出请用 printf("%lld")




全部评论

相关推荐

05-12 22:16
已编辑
北京邮电大学 研发工程师
牛客30236098...:0offer+1 滴滴都不给我面 佬没投鹅吗,鹅应该很喜欢北邮吧
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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