数据结构:模拟栈

数据结构:模拟栈

题目描述

输入样例

复制代码
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;
}
相关推荐
HUST几秒前
C 语言 第八讲:VS实用调试技巧
运维·c语言·开发语言·数据结构·算法·c#
历程里程碑9 分钟前
LeetCode128:哈希集合巧解最长连续序列
开发语言·数据结构·c++·算法·leetcode·哈希算法·散列表
风筝在晴天搁浅12 分钟前
hot100 160.相交链表
数据结构·链表
阿拉伯柠檬32 分钟前
应用层协议HTTP
linux·网络·c++·网络协议·http
爱上解放晚晚35 分钟前
QT转vs
c++
Yzzz-F43 分钟前
CF GYM105316A DP
数据结构·算法
C雨后彩虹1 小时前
幼儿园分班
java·数据结构·算法·华为·面试
Yupureki1 小时前
《算法竞赛从入门到国奖》算法基础:入门篇-二分算法
c语言·开发语言·数据结构·c++·算法·visual studio
游戏23人生1 小时前
c++ 语言教程——16面向对象设计模式(五)
开发语言·c++·设计模式