C语言实现链栈

#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR -1
typedef int Status;
typedef int SElemType;
typedef struct StackNode{
    SElemType data;
    struct StackNode *next;
}StackNode,*StackLinkNode;

void InitStack(StackLinkNode &S){
    S = NULL;
    printf("init success\n");
}

Status Insert(StackLinkNode &S, SElemType e){
    StackLinkNode p;
    p = (StackLinkNode)malloc(sizeof(StackNode));
    p->data = e;
    p->next = S;
    S = p;
    return OK; 
}

Status Pop(StackLinkNode &S){
    StackLinkNode p;
    int e;
    e = S->data;
    p = S;

    S = S->next;
    free(p);
    return OK;
} 

void GetData(StackLinkNode S){
    StackLinkNode p;
    p = S;
    while(p){
        printf("data is %d\n",p->data);
        p = p->next;
    }
}


int main(){
    StackLinkNode S;
    InitStack(S);
    for (int i=0;i<10;i++){
        Insert(S,i+1);
    }
    Pop(S);
    Pop(S);
    GetData(S);
    return 0;
} 

 

posted @ 2022-05-20 12:00  Ccdjun  阅读(28)  评论(0编辑  收藏  举报