文章目录
- 栈的实现(链式栈)
- 栈的定义
- 初始化栈
- 进栈
- 判断是否为空栈
- 出栈
- 销毁栈
- 获取栈顶元素
- 获取栈的长度
- 栈的打印
- 完整代码(包括测试代码)
- Stack.h
- Stack.c
- test.c
栈的实现(链式栈)
首先新建一个工程:
Stack.h(链式栈的类型定义、接口函数声明、引用的头文件)
Stack.c(链式栈接口函数的实现)
test.c(主函数、测试栈各个接口功能)
完整的代码放在后面(包括测试代码),这里就不会展示测试的效果图。大家可以自己别敲边按测试代码测试。图解会写的很详细的,么么😙
栈的定义
typedef int ElemType; /* 链栈结点结构 */
typedef struct StackNode
{ElemType data;struct StackNode* next;
}StackNode;/* 链栈结构 */
typedef struct
{StackNode* top;int count;
}LinkStack;
初始化栈
// 初始化栈
LinkStack* Init()
{// 注意要给链栈分配内存LinkStack* p = (LinkStack*)malloc(sizeof(LinkStack));if (p == NULL){perror("malloc fail");exit(-1);}p->top = NULL; // 链栈的空其实就是 top=NULL 的时候p->count = 0;return p;
}
进栈
// 进栈
void push(LinkStack* stack, ElemType x)
{assert(stack);StackNode* s = (StackNode*)malloc(sizeof(StackNode));if (s ==NULL){perror("malloc fail");exit(-1);}s->data = x;s->next = stack->top; stack->top = s; stack->count++;
}
判断是否为空栈
这里可以用count是否等于0判断,也可以用stack->top=NULL来判断都可以。
// 判断是否为空栈
bool isEmpty(LinkStack* stack)
{assert(stack);return stack->count == 0;
}
出栈
// 出栈
void pop(LinkStack* stack)
{assert(stack);if (isEmpty(stack))return ;StackNode* q = stack->top;stack->top = stack->top->next;free(q); q = NULL;stack->count--;}
销毁栈
这里需要二级指针才能置空stack,但是我为了整体的美观,我们这里每次使用完后,在函数外面主动置空 plist。
// 销毁栈
void destroy(LinkStack* stack)
{assert(stack);StackNode* p = stack->top;StackNode* q;while (p){q = p;p = p->next;free(q);}stack->count = 0;q = NULL;free(stack);//stack = NULL;//需要主动释放置空,不然要使用二级指针}
获取栈顶元素
// 获取栈顶元素
ElemType getTop(LinkStack* stack)
{assert(stack);if (stack->top == NULL)exit(-1);elsereturn stack->top->data;}
获取栈的长度
// 获取栈的长度
int getLength(LinkStack* stack)
{assert(stack);return stack->count;
}
栈的打印
同顺序栈一样写在测试test.c文件中。
完整代码(包括测试代码)
Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>typedef int ElemType; /* 链栈结点结构 */
typedef struct StackNode
{ElemType data;struct StackNode* next;
}StackNode;/* 链栈结构 */
typedef struct
{StackNode* top;int count;
}LinkStack;// 初始化栈
LinkStack* Init();
// 进栈
void push(LinkStack* stack, ElemType x);
// 判断是否为空栈
bool isEmpty(LinkStack* stack);
// 出栈
void pop(LinkStack* stack);
// 销毁栈
void destroy(LinkStack* stack);
// 获取栈顶元素
ElemType getTop(LinkStack* stack);
// 获取栈的长度
int getLength(LinkStack* stack);
Stack.c
#define _CRT_SECURE_NO_WARNINGS 1#include"Stack.h"// 初始化栈
LinkStack* Init()
{// 注意要给链栈分配内存LinkStack* p = (LinkStack*)malloc(sizeof(LinkStack));if (p == NULL){perror("malloc fail");exit(-1);}p->top = NULL; // 链栈的空其实就是 top=NULL 的时候p->count = 0;return p;
}// 进栈
void push(LinkStack* stack, ElemType x)
{assert(stack);StackNode* s = (StackNode*)malloc(sizeof(StackNode));if (s ==NULL){perror("malloc fail");exit(-1);}s->data = x;s->next = stack->top; stack->top = s; stack->count++;
}// 判断是否为空栈
bool isEmpty(LinkStack* stack)
{assert(stack);return stack->count == 0;
}// 出栈
void pop(LinkStack* stack)
{assert(stack);if (isEmpty(stack))return ;StackNode* q = stack->top;stack->top = stack->top->next;free(q); q = NULL;stack->count--;}// 销毁栈
void destroy(LinkStack* stack)
{assert(stack);StackNode* p = stack->top;StackNode* q;while (p){q = p;p = p->next;free(q);}stack->count = 0;q = NULL;free(stack);//stack = NULL;//需要主动释放置空,不然要使用二级指针}// 获取栈顶元素
ElemType getTop(LinkStack* stack)
{assert(stack);if (stack->top == NULL)exit(-1);elsereturn stack->top->data;}// 获取栈的长度
int getLength(LinkStack* stack)
{assert(stack);return stack->count;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"int main()
{LinkStack* plist = Init();push(plist, 1);push(plist, 2);push(plist, 3);push(plist, 4);push(plist, 5);while (!isEmpty(plist)){printf("%d ", getTop(plist));pop(plist);}printf("\n");int len=getLength(plist);printf("%d\n", len);bool b = isEmpty(plist);if (b){printf("true\n");}else{printf("false\n");}destroy(plist);//主动置空plist = NULL;return 0;
}