描述
请你实现一个循环队列,该循环队列可利用的空间大小等于nn个int型变量的大小。
操作:
push x:将xx加入到循环队列尾端。若循环队列已满,输出"full"(不含引号),否则不输出任何内容。保证xx为int型整数。
front:输出队首元素,队首不出队。若队列为空,输出"empty"(不含引号)。
pop:输出队首元素,且队首出队。若队列为空,输出"empty"(不含引号)。
操作:
push x:将xx加入到循环队列尾端。若循环队列已满,输出"full"(不含引号),否则不输出任何内容。保证xx为int型整数。
front:输出队首元素,队首不出队。若队列为空,输出"empty"(不含引号)。
pop:输出队首元素,且队首出队。若队列为空,输出"empty"(不含引号)。
输入描述:
第一行输入两个整数n,qn,q (1\le n,q \le 10^51≤n,q≤105),表示循环队列可利用的空间大小和操作次数。
接下来的qq行,每行一个字符串,表示一个操作。保证操作是题目描述中的一种。
接下来的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")

京公网安备 11010502036488号