AcWing 829. 模拟队列
原题链接
简单
作者:
Hope_
,
2021-06-06 20:53:40
,
所有人可见
,
阅读 260
每个函数都写出来的代码..
C++ 代码
#include<iostream>
#include<string>
using namespace std;
const int N=1e5+10;
int m,queue[N],ll=0,rr=-1;//这样赋值是为了不插入的时候直接判断空 对头0 队尾-1
//向队尾插入一个数 x
void Push(int x){
queue[++rr]=x;
}
//从队头弹出一个数
void PoP(){
ll++;
}
//判断队列是否为空
void IsEmpty(){
if(ll<=rr) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
//查询队头元素
int Query(){
cout<<queue[ll]<<endl;
}
int main(){
cin>>m;
string s;
int x;
while(m--){
cin>>s;
if(s=="push"){
cin>>x;
Push(x);
}
else if(s=="pop"){
PoP();
}
else if(s=="empty"){
IsEmpty();
}
else{
Query();
}
}
return 0;
}