每天一个小程序(6)—— 链栈

来源:互联网 发布:淘宝x1手柄 编辑:程序博客网 时间:2024/05/19 13:27

#include <stdio.h>#include <stdlib.h>typedef int datatype;typedef struct LStack{datatype data;struct LStack *next;}StackNode,*LinkStack;//初始化LinkStack Init_LinkStack(){return NULL;}//判断栈空int Empty_LinkStack(LinkStack top){if(top == NULL) return 1;elsereturn 0;}//入栈LinkStack Push_LinkStack(LinkStack top,datatype x){LinkStack s;s = (LinkStack)malloc(sizeof(StackNode));s->data = x;s->next = top;top = s;return top;}//出栈LinkStack Pop_LinkStack(LinkStack top,datatype *x){LinkStack p;if(top == NULL)return NULL;else{*x = top->data;p = top;top = top->next;free(p);}return top;}void print(LinkStack top){while(!Empty_LinkStack(top)){printf("%d ",top->data);top = top->next;}}void main(){LinkStack top = Init_LinkStack();int data;printf("入栈\n");scanf("%d",&data);while(data != -1){top = Push_LinkStack(top,data);scanf("%d",&data);}printf("出栈\n");while(!Empty_LinkStack(top)){top = Pop_LinkStack(top,&data);printf("%d ",data);}}


小 结  

1.  栈是限定仅能在一端进行插入、删除的线性表;
2.  栈的元素具有后进先出的特点;
3.  栈顶元素的位置由一个称为栈顶指针的变量指示, 

4.  进栈、出栈操作要修改栈顶指针。


0 0
原创粉丝点击