数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
C++ 老炮儿的技术栈4 小时前
volatile使用场景
linux·服务器·c语言·开发语言·c++
hz_zhangrl4 小时前
CCF-GESP 等级考试 2026年3月认证C++一级真题解析
开发语言·c++·gesp·gesp2026年3月·gespc++一级
Liu628884 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
波特率1152004 小时前
const关键字与函数的重载
开发语言·c++·函数重载
干啥啥不行,秃头第一名5 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
tobias.b5 小时前
计算机基础知识-数据结构
java·数据结构·考研
2301_807367195 小时前
C++中的解释器模式变体
开发语言·c++·算法
2301_819414307 小时前
C++与区块链智能合约
开发语言·c++·算法
不想看见4047 小时前
Valid Parentheses栈和队列--力扣101算法题解笔记
开发语言·数据结构·c++
老约家的可汗7 小时前
C/C++内存管理探秘:从内存分布到new/delete的底层原理
c语言·c++