#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct zhan{
datatype info;
struct zhan *next;
}node;
node* init()
{
return NULL;
}
int empty(node *top)
{
return top?0:1;
}
datatype read(node *top)
{
if(!top)
{
printf("\n链式栈为空!\n");
exit(1);
}
return top->info;
}
node* put(node *top)
{
node *p;
datatype x;
printf("请输入一系列数据(以0结尾):\n");
scanf("%d",&x);
while(x)
{
p=(node*)malloc(sizeof(node));
p->info=x;
if(!top)
p->next=NULL;
else
p->next=top;
top=p;
scanf("%d",&x);
}
return top;
}
void display(node *top)
{
node *p;
p=top;
if(!p)
{
printf("\n链式栈为空!\n");
exit(1);
}
while(p)
{
printf("%-5d",p->info);
p=p->next;
}
printf("\n");
}
node* push(node *top,datatype x)
{
node *p;
p=(node*)malloc(sizeof(node));
p->info=x;
p->next=top;
top=p;
return top;
}
node* pop(node *top)
{
node *p;
if(!top)
{
printf("\n链式栈 为空!\n");
return NULL;
}
p=top;
top=top->next;
free(p);
return top;
}
int main()
{
node *top;
datatype x;
top=init();
top=put(top);
display(top);
x=read(top);
printf("\n%d\n",x);
printf("请输入你想要插入的数据:");
scanf("%d",&x);
top=push(top,x);
display(top);
top=pop(top);
display(top);
return 0;
}
链式栈的实现(C语言数据结构)
最新推荐文章于 2024-10-17 23:22:01 发布