数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
Kuo-Teng几秒前
Mastering High-Concurrency Data Processing: A Deep Dive into BufferTrigger
java·数据结构
_F_y13 分钟前
二分:山脉数组的峰顶索引、寻找峰值、寻找旋转排序数组中的最小值、点名
c++·算法
Elias不吃糖16 分钟前
克隆图(LeetCode 133)——用数组做映射的 DFS 解法
c++·算法·leetcode·深度优先
不知所云,17 分钟前
1. 开篇简介
c++·vulkan
欧阳x天21 分钟前
类和对象(三)
c++
秋深枫叶红27 分钟前
嵌入式第二十三篇——数据结构基本概念
linux·数据结构·学习·算法
lilv6628 分钟前
visual studio 2026中C4996错误 ‘operator <<‘: 被声明为已否决
c++·ide·visual studio
Zsy_05100334 分钟前
【数据结构】二叉树介绍及C语言代码实现
c语言·数据结构·算法
谁刺我心36 分钟前
蓝桥杯C++常用STL
c++·算法·蓝桥杯
小白程序员成长日记42 分钟前
力扣每日一题 2025.11.30
数据结构·算法·leetcode