数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
亮子AI1 小时前
【Tiptap】如何使用 ordered list?
数据结构·list·tiptap
南莺莺1 小时前
二叉排序树的创建和基本操作---C++实现
数据结构·c++·算法··二叉排序树
仰泳的熊猫1 小时前
1061 Dating
数据结构·c++·算法·pat考试
Fcy6481 小时前
二叉搜索树(C++实现)
开发语言·数据结构·c++·二叉搜索树
CoderYanger1 小时前
A.每日一题——1523. 在区间范围内统计奇数数目
java·数据结构·算法·leetcode·职场和发展
surtr11 小时前
Round 1019(div2) CD
数据结构·c++·算法·贪心算法·stl
Tim_102 小时前
【C++入门】02、C++程序初识
开发语言·c++
冰西瓜6002 小时前
分治(二)算法设计与分析 国科大
数据结构·算法
小小晓.2 小时前
Pinely Round 2 (Div. 1 + Div. 2)
c++·算法