AcWing 828. 模拟栈
原题链接
简单
作者:
公孙无知
,
2024-09-12 17:35:09
,
所有人可见
,
阅读 2
#include<iostream>
#include<cstring>
using namespace std;
int s[100005];
char str[11];
int top =0;
void push(int x){
s[++top]=x;
}
void pop()
{ if (top > 0)
top--;
}
int query()
{if (top > 0)return s[top];
}
const char* empty()
{static char ans[10];
if(top==0)
{
strcpy(ans,"YES");
}
else{
strcpy(ans,"NO");
}
return ans;
}
int main()
{
int times;
scanf("%d",×);
for (int i=1;i<=times;i++)
{
scanf("%s",str);
if (strcmp(str, "push") == 0) {
int x;
scanf("%d", &x);
push(x);
} else if (strcmp(str, "pop") == 0) {
pop();
} else if (strcmp(str, "empty") == 0) {
printf("%s\n", empty());
} else if (strcmp(str, "query") == 0) {
int result = query();
if (result == -1) {
printf("Stack is empty\n"); // 栈为空时输出提示
} else {
printf("%d\n", result); // 打印整数
}
}
}
return 0;
}