zoukankan      html  css  js  c++  java
  • 栈的操作链表+数组版

    数组实现

    #include<cstdio>
    #include<cstdlib>
    #define MAXN 1000
    int isEmpty(const int top){
        return !(top+1);
    }
    void pop(int *s,int *top){
        s[(*top)--]=0;
    }
    void push(int *s,int *top){
        int x;
        scanf("%d",&x);
        s[++*top]=x;
    }
    void showStack(int *s,int top){
        int i;
        for(i=0;i<top+1;i++){
            if(i==0){
                printf("%d",s[i]);
            }
            else{
                printf(" %d",s[i]);
            }
        }
        puts("");
    }
    int main(){
        int *s=(int *)malloc(MAXN*sizeof(int)),top=-1;
        char str[20];
        while(~scanf("%s",str)){
            if(str[1]=='o'||str[1]=='O'){
                if(!isEmpty(top)){
                    pop(s,&top);
                }
                else{
                    puts("Empty");
                }
            }
            else if(str[1]=='u'||str[1]=='U'){
                push(s,&top);
            }
            showStack(s,top);
        }
        return 0;
    }
    

    链表实现

    /**
    c++链表栈
    push,pop,showStack,isEmpty;
    */
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<stack>
    #include<iostream>
    using namespace std;
    typedef struct node{
        int data;
        struct node *next;
    }NODE;
    NODE* push(NODE *top,int x){
        NODE *tmp=(NODE*)malloc(sizeof(NODE));
        tmp->data=x;
        tmp->next=NULL;
        if(!top){
            top=tmp;
            top->next=NULL;
        }
        else{
            tmp->next=top;
            top=tmp;
        }
        return top;
    }
    NODE* pop(NODE *top){
        NODE *tmp=top;
        top=top->next;
        free(tmp);
        return top;
    }
    void showStack(NODE*top){
        NODE *p=top;
        while(p){
            if(p->next){
                printf("%d ",p->data);
            }
            else{
                printf("%d
    ",p->data);
            }
            p=p->next;
        }
    }
    bool isEmpty(NODE *s){
        bool ret;
        if(s){
            ret=false;
        }
        else{
            ret=true;
        }
        return ret;
    }
    int main(){
        int x;
        char str[10];
        NODE *s=NULL;
        while(~scanf("%s",str)){
            if(str[1]=='o'||str[1]=='O'){///pop
                if(!isEmpty(s)){
                    s=pop(s);
                }
                else{
                    puts("empty");
                }
            }
            else if(str[1]=='u'||str[1]=='U'){///push
                scanf("%d",&x);
                s=push(s,x);
            }
            showStack(s);
        }
        return 0;
    }
    



  • 相关阅读:
    Luogu2751 [USACO Training4.2]工序安排Job Processing
    BZOJ4653: [Noi2016]区间
    BZOJ1537: [POI2005]Aut- The Bus
    CF1041F Ray in the tube
    POJ1186 方程的解数
    Luogu2578 [ZJOI2005]九数码游戏
    BZOJ2216: [Poi2011]Lightning Conductor
    CF865D Buy Low Sell High
    BZOJ1577: [Usaco2009 Feb]庙会捷运Fair Shuttle
    几类区间覆盖
  • 原文地址:https://www.cnblogs.com/Q1143316492/p/6260747.html
Copyright © 2011-2022 走看看