[AcWing 829] 模拟队列


点击查看代码
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int q[N];
int l = 0, r = 0;
void push(int x)
{
q[r] = x;
r ++;
}
void pop()
{
l ++;
}
bool empty()
{
return l == r;
}
int query()
{
return q[l];
}
int main()
{
int m;
cin >> m;
while (m --) {
string str;
cin >> str;
if (str == "push") {
int x;
cin >> x;
push(x);
}
if (str == "empty") {
if (empty()) printf("YES\n");
else printf("NO\n");
}
if (str == "query") printf("%d\n", query());
if (str == "pop") pop();
}
return 0;
}
- 使用数组模拟队列

浙公网安备 33010602011771号