数据结构C语言链栈

来源:互联网 发布:慕课平台有哪些 知乎 编辑:程序博客网 时间:2024/05/17 11:07
#include<stdio.h>#include<stdlib.h>#define OK 1#define ERROR 0typedef int SElemType ;typedef struct  SNode{   SElemType data;   struct SNode *next;}StackNode,*LinkStack;//初始化void Init_Stack_L(LinkStack L){   L=(StackNode*)malloc(sizeof(StackNode));L->next=NULL;}//Push:入栈int Push(LinkStack &L,SElemType e){LinkStack p;p=(StackNode*)malloc(sizeof(StackNode));if(p==NULL)  return ERROR;p->data=e;p->next=L; L=p;    return OK;}int Pop(LinkStack &L,SElemType &e){LinkStack p=L;L=p->next;e=p->data;free(p);return OK;}/*void Print(LinkStack &L) {int e;LinkStack p=L;    while(p){printf("%3d",p->data);p=p->next;}printf("\n");}*/void main() {LinkStack L;int e;Init_Stack_L(L);Push(L,1);Push(L,2);Push(L,3);Pop(L,e);printf("%d\n",e);Pop(L,e);printf("%d\n",e);  }

原创粉丝点击