链式栈的实现(C语言数据结构)

#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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值