题解 | #【模板】栈#

【模板】栈

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")

全部评论

相关推荐

07-22 11:12
门头沟学院 Java
不是,我就随手投的怎么还真发面试啊
皮格吉:大厂特别快的——来自已经被共享中
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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