#include <iostream>
#include <cstring>
using namespace std;
const int N =100010;
int str[N];
int tt=-1;
//向栈顶加上一个元素
void push(int x){
str[++tt]=x;
}
//弹出栈顶的一个元素
void pop(){
str[tt]=NULL;
tt--;
}
//判断是否栈里有值
int empty(){
if(tt>=0){
return -1;
}else{
return 1;
}
}
//查询栈的顶头元素
int query(){
return str[tt];
}
int main(){
int n;
char ch[10];
scanf("%d",&n);
while(n--){
int a;
scanf("%s",&ch);
if(n==0){
getchar();
}
if(strcmp(ch,"push")==0){
scanf("%d",&a);
push(a);
}
if(strcmp(ch,"pop")==0){
pop();
}
if(strcmp(ch,"query")==0){
int a =query();
printf("%d\n",a);
}
if(strcmp(ch,"empty")==0){
int a= empty();
if(a==-1){
printf("NO\n");
}
else{
printf("YES\n");
}
}
}
return 0;
}
写了一道用数组模拟栈的题目很简单就不解释啥了
对了,在写这个题目的时候,我发现了一个问题,就是说scanf("%s",&a);并不能把字符串输入进去,只能用于输入字符串数组,但是cin>>就可以