数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
LyaJpunov9 天前
深入理解 C++ volatile 与 atomic:五大用法解析 + 六大高频考点
c++·面试·volatile·atomic
小灰灰搞电子9 天前
Qt PyQt与PySide技术-C++库的Python绑定
c++·qt·pyqt
时空自由民.9 天前
C++ 不同线程之间传值
开发语言·c++·算法
Ray_19979 天前
C++二级指针的用法指向指针的指针(多级间接寻址)
开发语言·jvm·c++
亮亮爱刷题9 天前
飞往大厂梦之算法提升-7
数据结构·算法·leetcode·动态规划
_周游9 天前
【数据结构】_二叉树OJ第二弹(返回数组的遍历专题)
数据结构·算法
双叶8369 天前
(C语言)Map数组的实现(数据结构)(链表)(指针)
c语言·数据结构·c++·算法·链表·哈希算法
Jay_5159 天前
C++ STL 模板详解:由浅入深掌握标准模板库
c++·学习·stl
Cyrus_柯9 天前
C++(面向对象编程——继承)
开发语言·c++·算法·面向对象
Echo``9 天前
12.OpenCV—基础入门
开发语言·c++·人工智能·qt·opencv·计算机视觉