数据结构:模拟队列

数据结构:模拟队列

题目描述

在这里插入图片描述
输入样例

10
push 6
empty
query
pop
empty
push 3
push 4
pop
query
push 6

输出样例

NO
6
YES
4

参考代码

#include <iostream>

using namespace std;

const int N = 100010;

int q[N], hh, tt;

int m, x;
string op;

void init()
{
    hh = 0;
    tt = -1;
}

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

void pop()
{
    hh++;
}

string empty()
{
    return tt < hh ? "Yes" : "No";
}

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

int main()
{
    init();
    
    cin >> m;
    while (m--)
    {
        cin >> op;
        if (op == "push")
        {
            cin >> x;
            push(x);
        }
        else if (op == "pop")
        {
            pop();
        }
        else if (op == "empty")
        {
            cout << empty() << endl;
        }
        else
        {
            cout << query() << endl;
        }
    }
    return 0;
}





相关推荐

  1. 数据结构与算法 | 基础篇】环形数组模拟队列

    2024-06-05 19:36:02       38 阅读
  2. 数据结构-队列

    2024-06-05 19:36:02       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-05 19:36:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-05 19:36:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-05 19:36:02       87 阅读
  4. Python语言-面向对象

    2024-06-05 19:36:02       96 阅读

热门阅读

  1. 汽车电子专栏目录一览

    2024-06-05 19:36:02       20 阅读
  2. stm32和esp32硬件资源上有什么区别

    2024-06-05 19:36:02       26 阅读
  3. 微信小程序动画

    2024-06-05 19:36:02       29 阅读
  4. 防止重复调用

    2024-06-05 19:36:02       25 阅读
  5. HFish蜜罐实践:网络安全防御的主动出击

    2024-06-05 19:36:02       32 阅读