数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
晚风吹长发6 分钟前
初步了解Linux中的线程同步问题及线程安全和死锁与生产消费者模型
linux·运维·服务器·开发语言·数据结构·安全
一只专注api接口开发的技术猿8 分钟前
淘宝商品详情API的流量控制与熔断机制:保障系统稳定性的后端设计
大数据·数据结构·数据库·架构·node.js
逆龙泰氽12 分钟前
位运算和进制
c++
smj2302_7968265225 分钟前
解决leetcode第3826题.最小分割分数问题
数据结构·python·算法·leetcode
Laurence28 分钟前
从零到一构建 C++ 项目(IDE / 命令行双轨实现)
前端·c++·ide
我在人间贩卖青春33 分钟前
cout语句和cin语句
c++·cin·输入输出流·cout
数智工坊35 分钟前
【数据结构-线性表】2.3 双链表-循环链表-静态链表-顺序表和链表比较
数据结构·链表
Jiu-yuan38 分钟前
C++文件操作
c++
VT.馒头38 分钟前
【力扣】2705. 精简对象
javascript·数据结构·算法·leetcode·职场和发展·typescript
2301_763472581 小时前
实时系统下的C++编程
开发语言·c++·算法