用数组来表示队列,怎么表示呢?我们先假设hh为头,tt为尾,当弹出队头的时候我们只需要把hh加一下就连可以了,相反tt一样也可以
#include<iostream>
using namespace std;
const int N=1e5+10;
int a[N],tt=-1,hh=0;//hh头 tt尾
int main(){
int n;
string x;
cin>>n;
while (n -- ){
cin>>x;
int y;
if(x=="push"){
cin>>y;
a[++tt]=y;
}
else if(x=="pop")hh++;
else if(x=="empty")cout<<(hh<=tt?"NO":"YES")<<endl;
else cout<<a[hh]<<endl;
}
}