AcWing 829. 模拟队列
原题链接
简单
作者:
szywdwd
,
2021-05-13 20:02:47
,
所有人可见
,
阅读 180
#include <iostream>
#include <string>
using namespace std;
const int N = 100010;
int que[N];// 顺序队
int head, tail;// 队头指向队头元素的下一个位置、队尾指向队尾元素
void init() {
head = 0;
tail = -1;
}
void push(int x) {
que[++tail] = x;
}
void pop() {
++head;
}
bool empty() {
return tail + 1 == head;
}
int query() {
return que[head];
}
int main()
{
int M;
cin >> M;
init();
while(M--) {
string op;
cin >> op;
if(op == "push") {
int x;
cin >> x;
push(x);
}
else if(op == "pop") {
pop();
}
else if(op == "empty") {
if(empty()) cout << "YES" << endl;
else cout << "NO" << endl;
}
else if(op == "query") {
cout << query() << endl;
}
}
return 0;
}