C语言数据结构(四)---------------------------HStack

#include <stdbool.h>

#define STACK_ENTRY(node, type, member) \
    ((type*)((char*)(node) - (size_t)&((type*)0)->member))

struct h_stack_head {
    struct h_stack_head *next;
};
typedef void (*free_stack_node_func) (struct h_stack_head *node);

struct h_stack {
    struct h_stack_head *elements;
    free_stack_node_func free_func;
};

static inline bool hstack_is_empty(struct h_stack *stack)
{
    return stack->elements == NULL ? true : false;
}

static inline struct h_stack_head *hstack_pop(struct h_stack *stack)
{
    if (hstack_is_empty(stack))
        return NULL;

    struct h_stack_head *result = stack->elements;
    stack->elements = stack->elements->next;
    result->next = NULL;
    return result;
}

static inline void hstack_push(struct h_stack *stack, struct h_stack_head *element)
{
    element->next = stack->elements;
    stack->elements = element;
    return;
}

static inline struct h_stack_head *hstack_peek(struct h_stack *stack)
{
    return stack->elements;
}

static inline struct h_stack *hstack_create(free_stack_node_func free_func)
{
    struct h_stack *hstack = (struct h_stack*) malloc(sizeof(*hstack));
    hstack->free_func = free_func;
    return hstack;
}

static inline void hstack_free(struct h_stack *hstack)
{
    struct h_stack_head *node = hstack->elements;
    while (node) {
        struct h_stack_head *next = node->next;
        hstack->free_func(node);
        node = next;
    }
    return;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值