数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
蒙奇D索大1 小时前
【数据结构】考研数据结构核心考点:平衡二叉树(AVL树)详解——平衡因子与4大旋转操作入门指南
数据结构·笔记·学习·考研·改行学it
怎么没有名字注册了啊1 小时前
查找成绩(数组实现)
c++·算法
im_AMBER2 小时前
数据结构 04 栈和队列
数据结构·笔记·学习
AI+程序员在路上2 小时前
QT6中Combo Box与Combo BoxFont 功能及用法
c++·qt
L_09072 小时前
【Algorithm】Day-4
c++·算法·leetcode
煜3643 小时前
C++异常与智能指针
开发语言·c++
光头闪亮亮3 小时前
ZBar 环境搭建与快速入门指南
c++
闭着眼睛学算法3 小时前
【双机位A卷】华为OD笔试之【模拟】双机位A-新学校选址【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·javascript·c++·python·算法·华为od
卿摆摆3 小时前
【C++】string的模拟实现
开发语言·c++
玫瑰花店4 小时前
C++速通Lambda表达式
开发语言·c++