AcWing 829. 模拟队列
原题链接
简单
作者:
dsyami
,
2021-05-11 16:40:36
,
所有人可见
,
阅读 212
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int q[N], hh = 0, tt = -1;
//向队尾插入一个元素
void push(int x)
{
q[ ++ tt] = x;
}
//从队头弹出一个元素
void pop()
{
hh ++ ;
}
//查询队头元素
int query()
{
return q[hh];
}
//判断队列是否为空
bool empty()
{
return hh > tt;
}
int main()
{
int M, x;
string s;
cin >> M;
while(M -- )
{
cin >> s;
if(s == "push")
{
cin >> x;
push(x);
}
else if(s == "pop")
pop();
else if(s == "query")
cout << query() <<endl;
else if(s == "empty")
if(empty()) cout << "YES" << endl;
else cout << "NO" <<endl;
}
return 0;
}