美文网首页
数据结构c语言实现链栈

数据结构c语言实现链栈

作者: 甜柚小仙女 | 来源:发表于2019-01-27 12:10 被阅读0次
        #include<stdio.h>
    #include<stdlib.h>
    #define ElemType int 
    typedef struct StackNode{
        struct StackNode *next;
        ElemType data;
    }Node;
    typedef struct Stack{
        Node *top;
        int count;
    }LinkStack;
    
    LinkStack *createStack(){
        LinkStack *s ;
        s = (LinkStack*)malloc(sizeof(LinkStack));
        s->top = NULL;
        s->count = 0;
        return s;
    }
    int isNull(LinkStack *s){
        if(s->top==NULL){
            return 1;
        }
        return 0;
    }
    void pushLinkStack(LinkStack *s,ElemType ele){
        Node *p;
        p=(Node*)malloc(sizeof(Node));
        p->data = ele;
        p->next = s->top;
        s->top = p;
        s->count++;
    } 
    
    ElemType popLinkStack(LinkStack *s){
        if(!isNull(s)){
            ElemType res = s->top->data;
            s->top = s->top->next;
            s->count--;
            return res;
        }
        return -1;
    }
    void getCount(LinkStack *s){
        printf("%d\n",s->count);
    }
    void getTop(LinkStack *s){
        printf("%d\n",s->top->data);
    }
    int main(){
        LinkStack *s;
        s=createStack();
        int i;
        for(i=1;i<100;i++){
            pushLinkStack(s,i);
        }
        getTop(s);
        getCount(s);
        for(i=1;i<100;i++){
            printf("%d ",popLinkStack(s));
        }
    
    }

    相关文章

      网友评论

          本文标题:数据结构c语言实现链栈

          本文链接:https://www.haomeiwen.com/subject/rppgjqtx.html