数据结构学习系列之链式栈

  • 链式栈:
  • 即:栈的链式存储结构;
  • 分析:
  • 为了提高程序的运算效率,应采用头插法头删法
  • 进栈:
int push_link_stack(stack_t *link_stack,int data)
{
    if(NULL == link_stack)
    {
        printf("入参合理性检查\n");
        return -1;
    }

    node_t *pnew = (node_t *)malloc(sizeof(node_t));
    if(NULL == pnew)
    {
        printf("内存分配失败\n");
        return -1;
    }

    pnew->data = data;
    pnew->next = link_stack->top;
    link_stack->top = pnew;
    link_stack->count++;
    return 0;

}
  • 出栈:
int pop_link_stack(stack_t *link_stack,int *data)
{

    if(NULL == link_stack || NULL == data)
    {
        printf("入参合理性检查\n");
        return -1;

    }
    if(link_stack_is_empty(link_stack))
    {

        printf("此时栈为空,出栈失败\n");
        return -1;

    }


    *data = link_stack->top->data;
    node_t *pdel = link_stack->top;
    link_stack->top = pdel->next;
    free(pdel);
    pdel = NULL;
    link_stack->count--;
    return 0;
}

  • 示例代码:
  • link_stack.h:
#ifndef __LINK_STACK_H__
#define __LINK_STACK_H__


#include 
#include 
#include 

//链表结点的结构体
typedef struct NODE
{
    int data;
    struct NODE *next;

}node_t;

//链式栈的结构体

typedef struct Link_stack
{
    unsigned int count;
    node_t *top;

}stack_t;

int create_link_stack(stack_t **link_stack);
int push_link_stack(stack_t *link_stack,int data);
int link_stack_is_empty(stack_t *link_stack);
int pop_link_stack(stack_t *link_stack,int *data);
int clean_link_stack(stack_t *link_stack);
int destory_link_stack(stack_t **link_stack);
int print_link_stack(stack_t *link_stack);

#endif
  • link_stack.c:
#include "link_stack.h"

int create_link_stack(stack_t **link_stack)
{
    if(NULL == link_stack)
    {
        printf("入参合理性检查\n");
        return -1;
    }

    *link_stack = (stack_t *)malloc(sizeof(stack_t));

    if(NULL == *link_stack)
    {
        printf("内存分配失败\n");
        return -1;
    }

    memset(*link_stack,0,sizeof(stack_t));
    return 0;

}

//进栈
int push_link_stack(stack_t *link_stack,int data)
{
    if(NULL == link_stack)
    {
        printf("入参合理性检查\n");
        return -1;
    }

    node_t *pnew = (node_t *)malloc(sizeof(node_t));
    if(NULL == pnew)
    {
        printf("内存分配失败\n");
        return -1;
    }

    pnew->data = data;
    pnew->next = link_stack->top;
    link_stack->top = pnew;
    link_stack->count++;
    return 0;

}
//判断栈是否为空
int link_stack_is_empty(stack_t *link_stack)
{
    if(NULL == link_stack)
    {
        printf("入参合理性检查\n");
        return -1;

    }

    return link_stack->top == NULL ? 1 : 0;


}
//出栈
int pop_link_stack(stack_t *link_stack,int *data)
{

    if(NULL == link_stack || NULL == data)
    {
        printf("入参合理性检查\n");
        return -1;

    }
    if(link_stack_is_empty(link_stack))
    {

        printf("此时栈为空,出栈失败\n");
        return -1;

    }


    *data = link_stack->top->data;
    node_t *pdel = link_stack->top;
    link_stack->top = pdel->next;
    free(pdel);
    pdel = NULL;
    link_stack->count--;
    return 0;
}

//清空栈
int clean_link_stack(stack_t *link_stack)
{
    if(NULL == link_stack)
    {
        printf("入参合理性检查\n");
        return -1;

    }

    //循环头删
    node_t *pdel = NULL;
    while(link_stack->top != NULL)
    {
        pdel = link_stack->top;
        link_stack->top = pdel->next;
        free(pdel);
        pdel = NULL;

    } 
    link_stack->count = 0;
    return 0;


}

//销毁栈
int destory_link_stack(stack_t **link_stack)
{
    if(NULL == link_stack)
    {
        printf("入参合理性检查\n");
        return -1;

    }

    clean_link_stack(*link_stack);
    free(*link_stack);
    *link_stack = NULL;

    return 0;
}

//遍历栈中数据
int print_link_stack(stack_t *link_stack)
{
    if(NULL == link_stack)
    {
        printf("入参合理性检查\n");
        return -1;

    }
    
    node_t *ptemp = link_stack->top;
    while(ptemp != NULL)
    {
        printf("%d ",ptemp->data);
        ptemp = ptemp->next;
    }
    puts("");
    return 0;
}

  • main.c:
#include "link_stack.h"

int main(int argc, char const *argv[])
{
    stack_t *link_stack = NULL;

    create_link_stack(&link_stack);
    printf(" link_stack = %p\n", link_stack);

    // 进栈
    push_link_stack(link_stack, 22);
    push_link_stack(link_stack, 22);
    push_link_stack(link_stack, 11);
    push_link_stack(link_stack, 22);
    push_link_stack(link_stack, 22);
    push_link_stack(link_stack, 77);
    push_link_stack(link_stack, 22);

    print_link_stack(link_stack);

    // 出栈
    int data;
    pop_link_stack(link_stack, &data);
    printf("%d ", data);
    pop_link_stack(link_stack, &data);
    printf("%d ", data);
    pop_link_stack(link_stack, &data);
    printf("%d ", data);
    pop_link_stack(link_stack, &data);
    printf("%d ", data);
    pop_link_stack(link_stack, &data);
    printf("%d ", data);
    pop_link_stack(link_stack, &data);
    printf("%d ", data);
    pop_link_stack(link_stack, &data);
    printf("%d ", data);

    puts("");


    pop_link_stack(link_stack, &data);
   
    

    clean_link_stack(link_stack);
    print_link_stack(link_stack);

    destory_link_stack(&link_stack);
    printf(" link_stack = %p\n", link_stack);

    return 0;
}

  • 运行结果:
 link_stack = 0x564e05f39260
22 77 22 22 11 22 22 
22 77 22 22 11 22 22 
此时栈为空,出栈失败

 link_stack = (nil)
  • 本示例代码仅供参考;

你可能感兴趣的:(数据结构学习系列,数据结构,学习,算法,链式栈,c语言,青少年编程)