队列,先进先出。
hh代表头,tt代表尾
#include<iostream>
using namespace std;
const int N=1e5+10;
int q[N],tt,hh;
void init()
{
tt=-1;
hh=0;
}
void push(int x)
{
q[++tt]=x;
}
void pop()
{
hh++;
}
bool empty()
{
if(hh<=tt) return true;
else
return false;
}
int query()
{
return q[hh];
}
int main()
{
int m;
cin>>m;
init();
while(m--)
{
int x;
string s;
cin>>s;
if(s=="push")
{
cin>>x;
push(x);
}
else if(s=="empty")
{
if(empty())
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
else if(s=="pop")
{
pop();
}
else
{
cout<<query()<<endl;
}
}
return 0;
}
Acwing 829 模拟队列
最新推荐文章于 2022-10-27 22:33:55 发布