2.26链栈

#include "link_stack.h"
//申请栈顶指针
top_p create_top()
{
    top_p top = (top_p)malloc(sizeof(top_t));
    if(top==NULL)
    {
        printf("空间申请失败\n");
        return NULL;
    }
    top->len = 0;
    top->ptop = NULL;  //刚申请栈指针时没有指向元素
    return top;
}
//申请结点的函数
link_p create_node(int data)
{
    link_p new = (link_p)malloc(sizeof(link_stack));
    if(new==NULL)
    {
        printf("申请空间失败\n");
        return NULL;
    }
    new->data = data;
    return new;
}
//入栈/压栈
void push_stack(top_p T,int data)
{
    if(T==NULL)
    {
        printf("入参为空\n");
        return;
    }
    link_p new = create_node(data);
    //入栈
    new->next = T->ptop;
    T->ptop = new;
    T->len++;
}
//判空
int empty(top_p T)
{
    if(T==NULL)
    {
        printf("入参为空\n");
        return;
    }
    return T->ptop==NULL?1:0;

}
//出栈/弹栈
void pop_stack(top_p T)
{
    if(T==NULL)
    {
        printf("入参为空\n");
        return;
    }
    link_p del=T
    T->next->next=T->next->ptopi;
    free(del);
}
//遍历
void show_stack(top_p T)
{
    if(T==NULL)
    {
        printf("入参为空\n");
        return;
    }
    link_p p=T;
    while(p!=NULL)
    {
        printf("%d",p->data);
        p=p->next;
    }
}
//销毁
void free_stack(top_p T)
void free_stack(top_p T)
{
    if (T == NULL)
    {
        return;
    }
    link_p p = T->ptop;
    link_p del;
 
    while (p != NULL)
    {
        delNode=p;
        p=p->next;
        free(delNode);
        T->len--;
        del=NULL;
    }
}

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值