题解 | #【模板】栈#

【模板】栈

https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf

#include <stdio.h>
#include <string.h>
#define MAX 200000
int stack[MAX];
int stack_i=0;//0为空
void push(int n){
    if(stack_i<MAX)
    stack[stack_i++]=n;
}
void pop(){
    if(stack_i>0){
        printf("%d\n",stack[--stack_i]);
    }
    else{
        printf("error\n");
    }
}
void top(){
    if(stack_i>0){
        printf("%d\n",stack[stack_i-1]);
    }
    else{
        printf("error\n");
    }
}


int main() {
    int a, b;
    int n;
    char str[6];

    scanf("%d",&n);
    while(n--){
        scanf("%s",&str);
        if(strcmp(str, "push")==0){
            int num=0;
            scanf("%d",&num);
            push(num);
        }
        if(strcmp(str, "pop")==0){
            pop();
        }
        if(strcmp(str, "top")==0){
            top();
        }


    }



    return 0;
}

全部评论

相关推荐

机械打工仔:第一位颇有孟德之志
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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