数据结构:模拟栈

数据结构:模拟栈

题目描述

在这里插入图片描述
输入样例

10
push 5
query
push 6
pop
query
pop
empty
push 4
query
empty

输出样例

5
5
YES
4
NO

参考代码

#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;
}

相关推荐

  1. 数据结构与算法 | 基础篇】数组模拟

    2024-06-05 20:48:07       11 阅读
  2. 数据结构--

    2024-06-05 20:48:07       38 阅读
  3. 数据结构-

    2024-06-05 20:48:07       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-05 20:48:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-05 20:48:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-05 20:48:07       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-05 20:48:07       20 阅读

热门阅读

  1. MacOS下Python开发环境的深度构建与优化

    2024-06-05 20:48:07       10 阅读
  2. 爆字段名中select * from users as a join users解释

    2024-06-05 20:48:07       10 阅读
  3. lllllll

    2024-06-05 20:48:07       8 阅读
  4. 安卓手机APP开发___设置闹钟

    2024-06-05 20:48:07       10 阅读
  5. 外界的声音都是参考,你不开心就不要参考

    2024-06-05 20:48:07       13 阅读
  6. 网络协议学习笔记

    2024-06-05 20:48:07       11 阅读
  7. C++网络编程——实现一个简单的echo服务器

    2024-06-05 20:48:07       10 阅读