算法学习笔记(13)——队列

队列

数组模拟队列

题目链接:AcWing 829. 模拟队列

#include <iostream>

using namespace std;

const int N = 100010;

int n;
int q[N];
int hh = 0, tt = -1;

void push(int x) { q[++ tt] = x; }

void pop() { hh ++; }

bool empty() { return hh > tt; }

int query() { return q[hh]; }

int main()
{
    cin >> n;
    
    while (n -- ) {
        string op;
        cin >> op;
        if (op == "push") {
            int x;
            cin >> x;
            push(x);
        }
        else if (op == "pop") pop();
        else if (op == "empty") {
            if (empty()) puts("YES");
            else puts("NO");
        }
        else {
            cout << query() << endl;
        }
    }
    
    return 0;
}
posted @ 2022-12-09 22:06  S!no  阅读(22)  评论(0编辑  收藏  举报