数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

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;
}
相关推荐
White の algo8 分钟前
【C++初阶】内存管理
开发语言·c++
张胤尘18 分钟前
算法每日一练 (9)
数据结构·算法
头发尚存的猿小二1 小时前
二叉树(顺序结构:堆)---数据结构
数据结构
osir.1 小时前
2025天梯训练1
c++·多关键字最短路
Zach_yuan1 小时前
list的模拟实现
c++·list
胡桃不是夹子1 小时前
学会了蛇形矩阵
c++·算法·矩阵
结衣结衣.1 小时前
【Qt】自定义信号和槽函数
开发语言·c++·qt·c++11
qq_433554542 小时前
C++ 二叉搜索树代码
开发语言·c++·算法
橘颂TA2 小时前
每日一练之移除链表元素
数据结构·链表
Coder Zhang2 小时前
后序线索化二叉树,并找到指定结点前驱,非递归逆序输出
数据结构·算法