数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
cany10004 小时前
C++ -- 可变参数模板
c++
不会C语言的男孩5 小时前
C++ Primer 第2章:变量和基本类型
开发语言·c++
云泽8087 小时前
C++ 可调用对象通关指南:深度解析 Lambda 表达式、function 包装器与 bind 绑定器
开发语言·c++·算法
Tri_Function7 小时前
简单图论大学习
c++
lqqjuly8 小时前
C++ 完整知识体系—从基础语法到现代 C++23 的系统性总结
c++·c++23
CS创新实验室8 小时前
从顺序表到动态数组:数据结构的永恒基石与现代语言的优雅封装
数据结构·算法
王老师青少年编程8 小时前
信奥赛C++提高组csp-s之FHQ Treap
c++·csp·平衡树·信奥赛·csp-s·提高组·fhq treap
8Qi810 小时前
LeetCode 23. 合并 K 个升序链表 —— 小顶堆(PriorityQueue)
数据结构·算法·leetcode·链表·
QiLinkOS10 小时前
《打破“用爱发电”:一种基于 Gitee 与时间戳的开源权益分配机制探索》
c语言·数据结构·c++·科技·算法·gitee·开源
Irissgwe10 小时前
c++STL--string类
c++·stl·string