数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
denggun123452 分钟前
悬垂指针 和 野指针
数据结构
Pluto_CSND16 分钟前
JSONPath解析JSON数据结构
java·数据结构·json
无限进步_17 分钟前
【C语言】用队列实现栈:数据结构转换的巧妙设计
c语言·开发语言·数据结构·c++·链表·visual studio
liu****22 分钟前
02_Pandas_数据结构
数据结构·python·pandas·python基础
optimistic_chen1 小时前
【Redis 系列】常用数据结构---Hash类型
linux·数据结构·redis·分布式·哈希算法
千里马-horse2 小时前
TypedArrayOf
开发语言·javascript·c++·node.js·napi
五阿哥永琪2 小时前
Redis的常用数据结构
数据结构·数据库·redis
YIN_尹2 小时前
【C++11】lambda表达式(匿名函数)
java·c++·windows
陳10302 小时前
C++:vector(2)
开发语言·c++
盖世灬英雄z2 小时前
数据结构与算法学习(一)
c++·学习·排序算法