数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
许长安7 小时前
c/c++ static关键字详解
c语言·c++·经验分享·笔记
Murphy_lx7 小时前
C++ thread类
开发语言·c++
月夜的风吹雨7 小时前
【C++ STL 深度剖析】:vector 底层模拟实现与核心陷阱解析
c++·vector·类和对象·visual studio
彩妙不是菜喵7 小时前
C++ 中 nullptr 的使用与实践:从陷阱到最佳实践
开发语言·jvm·c++
逐步前行8 小时前
C数据结构--排序算法
c语言·数据结构·排序算法
_dindong9 小时前
笔试强训:Week-4
数据结构·c++·笔记·学习·算法·哈希算法·散列表
liu****10 小时前
12.线程(二)
linux·开发语言·c++·1024程序员节
陌路2010 小时前
S12 简单排序算法--冒泡 选择 直接插入 希尔排序
数据结构·算法·排序算法
小冯的编程学习之路10 小时前
【C++】:C++基于微服务的即时通讯系统(2)
开发语言·c++·微服务
许长安10 小时前
C/C++中的extern关键字详解
c语言·开发语言·c++·经验分享·笔记