数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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 小时前
算法篇—反转链表
c语言·数据结构·算法·leetcode
小王C语言6 小时前
【7. 实现登录注册模块】:实现会话管理、注册/登录/退出 API
网络·c++
峥无9 小时前
C++11 深度详解:现代 C++ 基石全梳理
开发语言·c++·笔记
阿米亚波9 小时前
【C++ STL】std::unordered_multimap
开发语言·数据结构·c++·笔记·stl
小王C语言9 小时前
【3. 基于 Vibe Coding 的 OJ 平台】. 构建仓库、环境准备、需求梳理、安装依赖
网络·c++
小王C语言10 小时前
【8.进行接口测试】:通过 curl 进行接口自动化测试 / 通过 python 程序进行接口自动化测试
网络·c++
三克的油11 小时前
数据结构-4
数据结构
888CC++11 小时前
C++ 快速学习指南:从入门到进阶的实战路线
开发语言·c++
小小晓.11 小时前
C++记:函数
开发语言·c++·算法
驱动小百科12 小时前
Windows运行库合集下载 VC++、DirectX、.NET运行库安装教程
c++·windows·.net·windows运行库合集下载·windows运行库安装