模拟队列

实现一个队列,队列初始为空,支持四种操作:

  1. push x – 向队尾插入一个数 xx;
  2. pop – 从队头弹出一个数;
  3. empty – 判断队列是否为空;
  4. query – 查询队头元素。

现在要对队列进行 M 个操作,其中的每个操作 3 和操作 4 都要输出相应的结果。

#include <iostream>
using namespace std;
const int N = 100010;
int q[N];
int hh, tt = -1;

int main() {
    int n;
    cin >> n;
    while (n--) {
        string s;
        cin >> s;
        if (s == "push") {
            int x;
            cin >> x;
            q[++tt] = x;
        }
        else if (s == "empty") {
            if (hh <= tt) puts("NO");
            else puts("YES");
            
        }
        else if (s == "pop") {
            hh++;
        }
        else {
            cout << q[hh] << endl;
        }
    }
    return 0;
}

  

posted @ 2022-11-30 21:59  !&&||  阅读(34)  评论(0)    收藏  举报