数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
10
push 5
query
push 6
pop
query
pop
empty
push 4
query
empty

输出样例

复制代码
5
5
YES
4
NO

参考代码

cpp 复制代码
#include <iostream>

using namespace std;

const int N = 1000010;

int m, x;
int q[N];
string op;
int top;

void init()
{
    top = 0;
}

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

void pop()
{
    top--;
}

string empty()
{
   if (top == 0) return "Yes";
   return "No";
}


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

int main()
{
    init();
    
    cin >> m;
    while (m--)
    {
        cin >> op;
        if (op == "push")
        {
            cin >> x;
            push(x);
        }
        else if (op == "pop")
        {
            pop();
        }
        else if (op == "query")
        {
            cout << query() << endl;
        }
        else
        {
            cout << empty() << endl;
        }
    }
    return 0;
}
相关推荐
天威?*33 分钟前
团体程序设计天梯练习集L3-001 凑零钱
数据结构·c++
龙砚序1 小时前
【数据结构|栈实战 03-2】不带头结点链栈实现:文本编辑器撤销 Undo 模块(C 语言)
数据结构
充值内卷1 小时前
Varsar:一个 5.7 MB 的免安装文件对比工具
c++·比较工具
hai3152475431 小时前
阶乘分层关系模型存储文本与表格
数据结构·算法·rust·排序算法·线性回归
凌冰_2 小时前
数据结构 - 数组(八)
数据结构·算法·矩阵
wo_jack2 小时前
C++ 入门笔记(二十):stack / queue 的模拟实现 —— 适配器与 deque 底层
c++·stl·deque·queue·stack
_dindong2 小时前
仿muduo库实现高并发服务器 -通用类型Any类型的实现
服务器·c++
不在逃避q3 小时前
用生活里的小场景,一键打开 C++ 编程入门大门
开发语言·c++·生活
Shaoshing3 小时前
LeetCode 1798. 你能构造出连续值的最大数目
java·数据结构·算法
YSL0701243 小时前
C++string类详解
开发语言·数据结构·c++