- 链式栈:
- 即:栈的
链式存储
结构; - 分析:
- 为了提高程序的
运算效率
,应采用头插法
和头删法
; - 进栈:
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;
}
#ifndef __LINK_STACK_H__
#define __LINK_STACK_H__#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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
#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;
}
#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)