题解 | #【模板】栈#

【模板】栈

https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf?tpId=308&tqId=2111163&ru=/exam/oj&qru=/ta/algorithm-start/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D308

#include <stdio.h>
#include <string.h>
#define MAX 100000
int main()
{
    int n,top=-1;
    int stack[MAX]={0};
    scanf("%d",&n);
    while(n--){
        char a[10];
        scanf("%s",a);
        if(!strcmp(a, "push"))
        {
            if(top==MAX-1)
            return 0;
            int x;
            scanf("%d",&x);
            stack[++top] = x;
            continue;
        }
        if(!strcmp(a, "pop"))
        {
            if(top==-1)
            {
            printf("error\n");
            continue;}
            else
            {
            printf("%d\n",stack[top--]);
            continue;}
        }
        if(!strcmp(a, "top"))
        {
            if(top==-1)
            {
            printf("error\n");
            continue;
            }
            else{
            printf("%d\n",stack[top]);
            continue;}
        }
    }
    return 0;
}

全部评论

相关推荐

挂的真快
投递小鹏汽车等公司10个岗位
点赞 评论 收藏
分享
07-30 13:50
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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