题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
#include <stdio.h> #include<string.h> int main() { int n; int count = 0; char operand[5]; int num; int arr[100000]; scanf("%d", &n); for (size_t i = 0; i < n; i++) { scanf("%s", operand); if (!strcmp(operand, "push")) { scanf("%d", &num); arr[count++] = num; } else { if (count == 0) printf("error\n"); else if (!strcmp(operand, "pop")) printf("%d\n", arr[--count]); else if (!strcmp(operand, "top")) printf("%d\n", arr[count - 1]); } } return 0; }