AcWing 829. 模拟队列
原题链接
简单
作者:
时过境迁
,
2021-02-02 16:43:40
,
所有人可见
,
阅读 238
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 100010;
//初始化时头尾相等,插入元素是尾巴+1,插入数,即head下标的值为空,下一个值为队头元素
int queue[N], head, tail;
int m;
void init()
{
head = 0, tail = 0;
}
void push(int x)
{
tail++;
queue[tail] = x;
}
void pop()
{
head++;
}
bool empty()
{
return head == tail;
}
void query()
{
cout<<queue[head+1]<<endl;
}
int main()
{
init();
cin>>m;
while(m--)
{
string op;
int x;
cin>>op;
if(op == "push")
{
cin>>x;
push(x);
}
else if(op == "pop")
{
pop();
}
else if(op == "empty")
{
if(empty())
puts("YES");
else
puts("NO");
}
else
{
query();
}
}
return 0;
}