题解 | 【模板】栈的操作

【模板】栈的操作

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;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务