数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
王老师青少年编程15 分钟前
2024年3月GESP真题及题解(C++八级): 接竹竿
c++·题解·真题·gesp·csp·八级·接竹竿
偷星星的贼1116 分钟前
C++中的访问者模式实战
开发语言·c++·算法
雾岛听蓝30 分钟前
红黑树深度解析:设计原理与实现逻辑
c++
gjxDaniel35 分钟前
A+B问题天堂版
c++·算法·字符串·字符数组
M__3339 分钟前
动态规划进阶:简单多状态模型
c++·算法·动态规划
米优1 小时前
使用Qt实现消息队列中间件动态库封装
c++·中间件·rabbitmq
N.D.A.K1 小时前
CF2138C-Maple and Tree Beauty
c++·算法
AI视觉网奇1 小时前
ue 5.5 c++ mqtt 订阅/发布 json
网络·c++·json
程序员-King.1 小时前
day159—动态规划—打家劫舍(LeetCode-198)
c++·算法·leetcode·深度优先·回溯·递归
txinyu的博客1 小时前
解析muduo源码之 StringPiece.h
开发语言·网络·c++