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

【模板】循环队列

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")




全部评论

相关推荐

白火同学:大二有这水平很牛了,可以适当对关键信息加粗一点,比如关键技术、性能指标之类的。
点赞 评论 收藏
分享
06-15 02:05
已编辑
南昌航空大学 数据分析师
Eason三木:你如果想干技术岗,那几个发公众号合唱比赛的经历就去掉,优秀团员去掉,求职没用。然后CET4这种不是奖项,是技能,放到下面的专业技能里或者单独列一个英语能力。 另外好好改改你的排版,首行缩进完全没有必要,行间距好好调调,别让字和标题背景黏在一起,你下面说能做高质量PPT你得展现出来啊,你这简历排版我用PPT做的都能比你做的好。 然后自我评价,你如果要干数据工程师,抗压能力强最起码得有吧。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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