题解 | 【模板】栈的操作
【模板】栈的操作
https://www.nowcoder.com/practice/cdf02ea916454957b575585634e5773a
#include <stdio.h>
#include<string.h>
int main()
{
int n;
scanf("%d",&n);
long long int a[n];
char s[10];
int j=0,flag=0;
for(int i=0;i<n;i++)
{
scanf("%s",s);
if(strcmp(s,"push")==0)
{
scanf("%lld",&a[j]);
j++;
}
else if(strcmp(s,"pop")==0)
{
if(j==0)
{
printf("Empty\n");
continue;
}
else
{
j--;
}
}
else if(strcmp(s,"query")==0)
{
if(j==0)
{
printf("Empty\n");
continue;
}
else
{
printf("%lld\n",a[j-1]);
}
}
else
{
printf("%d\n",j);
}
}
return 0;
}
查看18道真题和解析
