首页 > 其他分享 >模拟队列

模拟队列

时间:2022-11-30 22:12:51浏览次数:36  
标签:队列 tt cin int hh 模拟 empty

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

  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;
}

  

标签:队列,tt,cin,int,hh,模拟,empty
From: https://www.cnblogs.com/leetothemoon/p/16939909.html

相关文章