题目描述:
代码模板:
int q[N],hh,tt = -1;
//插入操作
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];
}
AC代码:
#include<iostream>
using namespace std;
const int N = 100010;
int q[N],hh,tt = -1;
//插入操作
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;
scanf("%d", &m);
while(m --)
{
string str;
cin >> str;
if(str == "push")
{
int x;
scanf("%d", &x);
push(x);
}
else if(str == "pop")
{
pop();
}
else if(str == "empty")
{
bool flag = empty();
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
else
{
cout << query() << endl;
}
}
return 0;
}