AcWing 829. 模拟队列
原题链接
简单
作者:
落花已作风前舞
,
2024-04-26 21:25:15
,
所有人可见
,
阅读 3
#include <iostream>
using namespace std;
int const N = 1e5 + 10;
int q[N], hh = 0, tt = -1; // hh, tt为头结点和尾结点指针
int main ()
{
int m;
cin >> m;
while (m --)
{
string op;
cin >> op;
if (op == "push")
{
int x;
cin >> x;
q[++ tt] = x; // 压入一个元素, 队尾指针先向后移动一格,找到第一个空格,再将x压进去
}
else if (op == "pop") hh ++; // 从队头弹出一个元素,队头指针向前移一位
// else if (op == "empty") cout << (hh <= tt ? "NO" : "YES") << endl;
else if (op == "empty")
{
// if (hh == -1 && tt == -1) cout << "YES" << endl;
if (hh > tt) cout << "YES" << endl;
else if (hh <= tt ) cout << "NO" << endl;
}
else if (op == "query") cout << q[hh] << endl;
}
return 0;
}