链表的入栈出栈2

#include "12.h"

int main(int argc, const char *argv[])
{      int i;
    Pstack L=get_head();
    int a[6]={10,20,30,40,50,60};
    for(i=0;i<6;i++)
    {
    insert_stack(L,a[i]);
    }
    output(L);
    out_stack(L);
    output(L);
    des(L);
    output(L);
    return 0;
}
#include"12.h"
Pstack get_head()
{
    Pstack L=malloc(sizeof(Stack));
    if(L==NULL)
    {
        printf("申请失败\n");
        return NULL;
    }
    L->top=NULL;
    L->len=0;
    return L;
}
void insert_stack(Pstack L,int e)
{
    if(L==NULL)
    {
        printf("链栈为空或不存在\n");
        return;
    }
    Node *p=malloc(sizeof(Node));
    p->data=e;
    p->next=L->top;
    L->top=p;
    L->len++;
    printf("入栈成功\n");
}
void output(Pstack L)
{
    if(L==NULL||L->len==0)
    {
        printf("链栈为空或不存在\n");
        return;
    }
    int i;
    Node *t=L->top;
    for(i=0;i<L->len;i++)
    {
        printf("%d\t",t->data);
        t=t->next;
    }
    printf("\n");

}
void out_stack(Pstack L)
{if(L==NULL||L->len==0)
    {
        printf("链栈为空或不存在\n");
        return;
    }

    Node *p=L->top;
    printf("出栈的节点是%d\n",p->data);
    L->top=p->next;
    free(p);
    p=NULL;
    L->len--;
}
void des(Pstack L)
{if(L==NULL||L->len==0)
    {
        printf("链栈为空或不存在\n");
        return;
    }

    Node *p;
    while(L->top!=NULL)
    {
        p=L->top;
        L->top=L->top->next;
        free(p);
        L->len--;

    }
    free(L);
    L=NULL;
    printf("释放节点成功\n");
}
#ifndef _12_H_
#define _12_H_
#include<myhead.h>
#define MAX 6
typedef struct node
{
    int data;
    struct node *next;
}Node;
typedef struct
{
    int len;
    Node *top;
}Stack,*Pstack;
Pstack get_head();
void insert_stack(Pstack L,int e);
void output(Pstack L);
void out_stack(Pstack L);
void des(Pstack L);
#endif
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值