链式栈的表示和实现

表示

typedef struct node_t {
    
    data_t        data;
    struct node_t    *next;
    
} linknode_t, *linkstack_t;

实现

//创建
linkstack_t CreateEmptyLinkstack(void)
{
    linkstack_t stack;
    stack = (linkstack_t)malloc(sizeof(linknode_t));
    if (NULL != stack)
        stack->next = NULL;
    return stack;
}
//清空
int ClearLinkstack(linkstack_t stack)
{
    linknode_t *node;
    if (NULL != stack) {
        while (NULL != stack->next) {
            node = stack->next;
            stack->next = node->next;
            free(node);
        }
        return 0;
    } else {
        return -1;
    }
}
//销毁
int DestroyLinkstack(linkstack_t stack)
{
    if (NULL != stack) {
        ClearLinkstack(stack);
        free(stack);
    } else {
        return -1;
    }
}
//是否为空
int EmptyLinkstack(linkstack_t stack)
{
    if (NULL == stack) return -1;
    
    if (NULL != stack->next) {
        return 0;
    } else {
        return 1;
    }
}
//压栈
int PushStack(linkstack_t stack, data_t x)
{
    if (NULL == stack) return -1;
    
    linknode_t *stack_new;
    stack_new = (linkstack_t)malloc(sizeof(linknode_t));
    if (NULL == stack_new) return -1;
    stack_new->data = x;
    
    stack_new->next = stack->next;
    stack->next = stack_new;
    return 0;
}
//出栈
int PopStack(linkstack_t stack, data_t *x)
{
    if (NULL == stack) return -1;
    if (NULL == stack->next) return -1;
        
    linknode_t *node;
    node = stack->next;
    stack->next = node->next;
    if (NULL != x)
        *x = node->data;
    free(node);
    return 0;
    
}

测试代码

int Push_Pop(linkstack_t stack, data_t x);

/*
 * iterate through the stack, from the top to the base 
 * and print out info of each element
 */
void iterate_stack(linkstack_t stack)
{
    linknode_t *node; /* pointer to the node to be iterated */
    
    if (NULL == stack) return;

    printf("stack = top{");

    node = stack->next;
    while (NULL != node) {
        printf("%d->", node->data);
        node = node->next;
    }
    
    if (1 == EmptyLinkstack(stack))
        printf("}base
");
    else
        printf("}base
");
}

int main(int argc, char *argv[])
{
    linkstack_t stack;

    if (argc < 2) {
        printf("Usage: %s <len>
", argv[0]);
        return -1;
    }

    max_depth = atoi(argv[1]);
    
    stack = CreateEmptyLinkstack();

    if (NULL == stack) {
        printf("CreateEmptyLinkstack error
");
        return -1;
    }

    Push_Pop(stack, 1);

    DestroyLinkstack(stack);
    
    return 0;
}

int Push_Pop(linkstack_t stack, data_t x)
{
    data_t data_pop;
    static int    depth = 0;    

    if (depth == max_depth) {
        printf("----- reach the max depth of the stack!
");
        return 0;
    } else {
        depth++;
        
        printf("Push %d
", x); 
        PushStack(stack, x++);
        iterate_stack(stack);

        Push_Pop(stack, x);

        PopStack(stack, &data_pop);
        printf("Pop %d
", data_pop);
        iterate_stack(stack);

        return -1;
    }
}

结果 ./a.out 8 

Push 1
stack = top{1}base
Push 2
stack = top{2->1}base
Push 3
stack = top{3->2->1}base
Push 4
stack = top{4->3->2->1}base
Push 5
stack = top{5->4->3->2->1}base
Push 6
stack = top{6->5->4->3->2->1}base
Push 7
stack = top{7->6->5->4->3->2->1}base
Push 8
stack = top{8->7->6->5->4->3->2->1}base
----- reach the max depth of the stack!
Pop 8
stack = top{7->6->5->4->3->2->1}base
Pop 7
stack = top{6->5->4->3->2->1}base
Pop 6
stack = top{5->4->3->2->1}base
Pop 5
stack = top{4->3->2->1}base
Pop 4
stack = top{3->2->1}base
Pop 3
stack = top{2->1}base
Pop 2
stack = top{1}base
Pop 1
stack = top{}base
原文地址:https://www.cnblogs.com/vsyf/p/4915726.html