AcWing 828. 模拟栈
原题链接
简单
作者:
风起于浮萍之末
,
2024-01-28 14:30:16
,
所有人可见
,
阅读 34
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 100010;
int st[N];
int top=-1;
int n;
int main(){
scanf("%d",&n);// 输入一个操作数
while(n--){
string op;
// scanf("%s",op);
cin>>op;
if(op=="push"){
int a;
scanf("%d",&a);
st[++top]=a;// 注意是先移动 后赋值 ,因为是从1开始存储的
}
if(op=="pop"){
top--;//弹出一个元素,只需要移动下标即可,因为后面会覆盖
}
if(op=="query"){
printf("%d\n",st[top]);// 返回对头元素
}
if(op=="empty"){
cout << (top == -1 ? "YES" : "NO") << endl;
}
}
return 0;
}