[AcWing 829] 模拟队列

image
image


点击查看代码
#include<iostream>

using namespace std;
const int N = 1e5 + 10;
int q[N];
int l = 0, r = 0;
void push(int x)
{
    q[r] = x;
    r ++;
}
void pop()
{
    l ++;
}
bool empty()
{
    return l == r;
}
int query()
{
    return q[l];
}
int main()
{
    int m;
    cin >> m;
    while (m --) {
        string str;
        cin >> str;
        if (str == "push") {
            int x;
            cin >> x;
            push(x);
        }
        if (str == "empty") {
            if (empty())    printf("YES\n");
            else    printf("NO\n");
        }
        if (str == "query") printf("%d\n", query());
        if (str == "pop")   pop();
    }
    return 0;
}

  1. 使用数组模拟队列
posted @ 2022-04-30 00:37  wKingYu  阅读(34)  评论(0)    收藏  举报