数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
wangluoqi18 分钟前
c++ 树上问题 小总结
开发语言·c++
不梦闲人2 小时前
15 面向对象程序设计
c++
ArturiaZ3 小时前
【day29】
数据结构·c++·算法
锅包一切4 小时前
PART17 一维动态规划
c++·学习·算法·leetcode·动态规划·力扣·刷题
Polaris北4 小时前
第二十六天打卡
c++·算法·动态规划
Stringzhua5 小时前
队列-优先队列【Queue3】
java·数据结构·队列
汉克老师7 小时前
GESP2024年6月认证C++二级( 第三部分编程题(1) 平方之和)
c++·算法·预处理·完全平方数·循环结构·gesp二级·gesp2级
StandbyTime7 小时前
《算法笔记》练习记录-2.5-问题 C: 习题6-6 杨辉三角
c++·算法笔记
MR_Promethus7 小时前
【C++11】condition_variable 条件变量
c++·条件变量·并发编程
智者知已应修善业7 小时前
【排列顺序判断是否一次交换能得到升序】2025-1-28
c语言·c++·经验分享·笔记·算法