题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
#include <iostream> using namespace std; #include<stdlib.h> #include<string.h> #define MAX 10000 typedef struct{ int a[MAX]; int top1; }*Lstack; void inistack(Lstack s){ s->top1=0; } int main() { int n,x; int top=-1; cin>>n; long long int a[n]; string s; while(n>0){ cin>>s; if(s=="push"){ cin>>x; a[++top]=x; } else if(s=="top"){ if(top==-1){ cout<<"error"<<endl; }else{ cout<<a[top]<<endl; } } else if(s=="pop"){ if(top==-1){ cout<<"error"<<endl; }else{ cout<<a[top]<<endl; --top; } } n--; } } // 64 位输出请用 printf("%lld")