数据结构(C语言)——链式栈

链式栈的搭建

所谓链式栈,就是用链表的方式来实现栈的功能。

  • 链表:每个元素包含数据和下一个元素的指针
  • 栈:先入后出;相关概念:栈顶、栈底

功能:

  • 创建链式栈:
  • 进栈:
  • 出栈:
  • 判断是非为空栈:
  • 释放栈空间:

下面为实现的代码:

linkstck.h


typedef int data_t;

typedef struct node{
    data_t data;
    struct node *next;
}listnode, *linkstack;

linkstack stack_create();
int stack_push(linkstack s, data_t value);
data_t stack_pop(linkstack s);
int stack_empty(linkstack s);
data_t stack_top(linkstack s);
int stack_free(linkstack s);

linkstack.c

#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"


linkstack stack_create()
{
    linkstack s;
    s = (linkstack)malloc(sizeof(listnode));
    if (s ==NULL)
    {
        /* code */
        printf("malloc failed\n");
        return NULL;
    }
    
    s->data = 1;
    s->next = NULL;

    return s;

}

int stack_push(linkstack s, data_t value)
{
    linkstack p;

    if (s == NULL)
    {
        /* code */
        printf("s is NULL\n");
        return -1;
    }

    p = (linkstack)malloc(sizeof(listnode));
    if (p == NULL)
    {
        /* code */
        printf("malloc failed\n");
        return -1;
    }
    
    p->data = value;
    p->next = s->next;
    s->next = p;

    return 0; 
}

data_t stack_pop(linkstack s)
{
    linkstack p;
    data_t t;

    if (s == NULL)
    {
        /* code */
        printf("s is NULL\n");
        return -1;
    }

    p = s->next;
    s->next = p->next;

    t = p->data;

    free(p);
    p == NULL;

    return t;
}

int stack_empty(linkstack s)
{
    if (s == NULL)
    {
        /* code */
        printf("s is NULL\n");
        return -1;
    }

    return (s->next == NULL ? 1 : 0);
}

data_t stack_top(linkstack s)
{
    return (s->next->data);

}

int stack_free(linkstack s)
{
    linkstack p;

    while (s != NULL)
    {
        /* code */
        p = s;
        s = s->next;
        printf("free : %d\n", p->data);
        free(p);
    }
    printf("s is NULL\n");

    return NULL;
}

test.c

#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"

int main(int argc, char const *argv[])
{
    /* code */
    linkstack s;
    data_t top_value;

    s = stack_create();
    if (s == NULL)
    {
        /* code */
        return -1;
    }
    else{
        printf("linkstack create successful\n");
    }

    stack_push(s, 10);
    stack_push(s, 20);
    stack_push(s, 30);
    stack_push(s, 40);

    top_value = stack_top(s);
    printf("topvalue is %d\n", top_value);

    while (!stack_empty(s))
    {
        /* code */
        printf("pop:%d\n", stack_pop(s));
    }

    stack_free(s);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我见山河如故

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值