数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
txinyu的博客7 分钟前
HTTP服务实现用户级窗口限流
开发语言·c++·分布式·网络协议·http
代码村新手7 分钟前
C++-类和对象(上)
开发语言·c++
txinyu的博客18 分钟前
map和unordered_map的性能对比
开发语言·数据结构·c++·算法·哈希算法·散列表
im_AMBER36 分钟前
Leetcode 101 对链表进行插入排序
数据结构·笔记·学习·算法·leetcode·排序算法
予枫的编程笔记37 分钟前
【Java集合】深入浅出 Java HashMap:从链表到红黑树的“进化”之路
java·开发语言·数据结构·人工智能·链表·哈希算法
mjhcsp43 分钟前
C++ 后缀数组(SA):原理、实现与应用全解析
java·开发语言·c++·后缀数组sa
hui函数44 分钟前
如何解决 pip install 编译报错 ‘cl.exe’ not found(缺少 VS C++ 工具集)问题
开发语言·c++·pip
X在敲AI代码1 小时前
leetcodeD3
数据结构·算法
余瑜鱼鱼鱼1 小时前
Java数据结构:从入门到精通(十一)
数据结构
码农小韩1 小时前
基于Linux的C++学习——循环
linux·c语言·开发语言·c++·算法