一、题干:acwing模拟队列
二、代码实现
使用数组模拟队列和模拟栈十分相似,唯一的区别是多了一个头结点hh,以及tt初始化时从-1开始。
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int d[N],hh,tt = -1;
int main()
{
int m;
cin>>m;
string op;
while(m--)
{
cin>>op;
if(op == "push")
{
int x;
cin>>x;
d[++ tt] = x;
}
else if(op == "pop")
{
hh ++;
}
else if(op == "empty")
{
string s = hh <= tt ? "NO" : "YES";
cout<<s<<'\n';
}
else
{
cout<<d[hh]<<'\n';
}
}
return 0;
}