[AcWing 828] 模拟栈

image
image


点击查看代码
#include<iostream>

using namespace std;
const int N = 1e5 + 10;
int s[N], idx = 0;
void push(int x)
{
    s[idx] = x;
    idx ++;
}
void pop()
{
    idx --;
}
bool empty()
{
    return idx == 0;
}
int query()
{
    return s[idx - 1];
}
int main()
{
    int m;
    scanf("%d", &m);
    while (m --) {
        string str;
        cin >> str;
        if (str == "push") {
            int x;
            cin >> x;
            push(x);
        }
        if (str == "query") {
            printf("%d\n", query());
        }
        if (str == "pop") {
            pop();
        }
        if (str == "empty") {
            if (empty())    printf("YES\n");
            else    printf("NO\n");
        }
    }
    return 0;
}

  1. 使用数组模拟栈
posted @ 2022-04-29 23:26  wKingYu  阅读(34)  评论(0)    收藏  举报