栈(1)----用链表实现栈


一、定义

typedef  struct StackElement_t_{
    void *data;
    struct StackElement_t *next;
} StackElement_t;

typedef struct Stack_t_ {
    int size;
    int capacity;
    int (*destory)(void *data);
    StackElement_t *head;
} Stack_t;


typedef  struct StackElement_t_{

    void *data;

    struct StackElement_t *next;

} StackElement_t;


typedef struct Stack_t_ {

    int size;

    int capacity;

    int (*destory)(void *data);

    StackElement_t *head;

} Stack_t;


二、初始化:

int stack_init( Stack_t *st, int maxsize, int( *destory)(void *data)){
    st->size = 0;
    st->capacity = maxsize;
    st->destory = destory;
    st->head = NULL;
    return 0;
}


int stack_init( Stack_t *st, int maxsize, int( *destory)(void *data)){

    st->size = 0;

    st->capacity = maxsize;

    st->destory = destory;

    st->head = NULL;

    return 0;

}


三、入栈

int stack_push( Stack_t *st, const void *data){
    if( stack_is_full( st) )
        return OVERFLOW_ERROR;

    StackElement_t *new_element = NULL;
    new_element->data = data;
    new_element->next = st->head;
    st->head = st->head->next;
    st->size++;
    return 0;
}


int stack_push( Stack_t *st, const void *data){

    if( stack_is_full( st) )

        return OVERFLOW_ERROR;


    StackElement_t *new_element = NULL;

    new_element->data = data;

    new_element->next = st->head;

    st->head = st->head->next;

    st->size++;

    return 0;

}


四、出栈

int  stack_pop( Stack_t *st){
    if( stack_is_empty( st ) )
        return -1;

    void *data = stack_top( st );
    StackElement_t *del_element = st->head;
    st->destroy( data );
    free( del_element);
    del_element = NULL;
    st->size--;

    return 0;
}


int  stack_pop( Stack_t *st){

    if( stack_is_empty( st ) )

        return -1;


    void *data = stack_top( st );

    StackElement_t *del_element = st->head;

    st->destroy( data );

    free( del_element);

    del_element = NULL;

    st->size--;


    return 0;

}


五、栈顶元素

void *stack_top( Stack_t *st){
    if( stack_is_empty( st ))
        return NULL;

    return st->head->data;
}


void *stack_top( Stack_t *st){

    if( stack_is_empty( st ))

        return NULL;


    return st->head->data;

}


六、销毁栈

int stack_destroy( Stack_t *st){
    while( st->size > 0){
        stack_pop( st );
    }
    return 0;
}


int stack_destroy( Stack_t *st){

    while( st->size > 0){

        stack_pop( st );

    }

    return 0;

}


七、其他


(1)判断栈是否为空

int stack_is_empty( Stack_t *st)
{
    return (st->size == 0 );
}


int stack_is_empty( Stack_t *st)

{

    return (st->size == 0 );

}

(2)判断栈是否满

int stack_is_full( Stack_t *st )
{
    return (st->size = st->capacity);
}


int stack_is_full( Stack_t *st )

{

    return (st->size = st->capacity);

}


(3)返回栈的实际长度

int stack_size( Stack_t *st)
{
    return st->size;
}


int stack_size( Stack_t *st)

{

    return st->size;

}


(4)返回栈的容量

int stack_capacity( Stack_t *st)
{
    return st->capacity;
}


int stack_capacity( Stack_t *st)

{

    return st->capacity;

}
















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值