题解 | #【模板】栈#
【模板】栈
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; }