C/C++教程

[AcWing 829] 模拟队列

本文主要是介绍[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. 使用数组模拟队列
这篇关于[AcWing 829] 模拟队列的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!