C语言 数据结构 【栈】动态模拟实现
引言
动态模拟实现栈的各个接口
一、栈的概念与结构
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(LastInFirstOut)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
栈底层结构选型
栈的实现⼀般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。而且与链表的结构体相比,数组所占内存的较小。
二、栈的模拟实现
因为栈是用动态数组来模拟实现的,所以顺序表要是会的话,这个是很简单的
分三个文件来写:以代码注释为主
test.c //测试文件,测试栈的接口是否正确
Stack.h //实现栈所需要的所有的头文件(核心)
Stack.c //实现栈各个接口的代码(核心)
1、在Stack.h中定义栈的结构:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int STDataType; //取别名,方便以后修改数据类型
typedef struct Stack
{
STDataType* arr; //数组
int top; //栈的有效元素个数
int capacity; //栈的总大小
}ST; //取别名,方便下面代码的书写
2、栈的初始化、入栈
//初始化
void StackInit(ST* ps)
{
ps->arr = NULL; //先将数组指针设置为NULL
ps->top = ps->capacity = 0; //都是0
}
//入栈 -- 栈顶入栈
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity) //空间不够先增容
{
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity; //以二倍方式扩容
STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail!");
exit(1);
}
ps->arr = tmp; //将扩容后的地址给Stack的arr
ps->capacity = newCapacity; //修改空间大小;
}
ps->arr[ps->top++] = x; //赋值
}
3、判断栈是否为空、出栈操作
//判断栈是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//出栈 -- 栈顶出栈
void StackPop(ST* ps)
{
assert(!StackEmpty(ps));
--ps->top; //将栈顶减一
}
4、取栈顶元素(不出栈)、获取栈中有效元素个数
//取栈顶元素(不出栈)
STDataType StackTop(ST* ps)
{
assert(!StackEmpty(ps));
return ps->arr[ps->top - 1];
}
//获取栈中有效元素个数
int StackSize(ST* ps)
{
return ps->top;
}
5、销毁栈
//摧毁
void StackDestory(ST* ps)
{
if (ps->arr)
free(ps->arr);
ps->arr = NULL;
ps->top = ps->capacity = 0;
}
3、所有代码:
Stack.h中的代码:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType; //取别名,方便以后修改数据类型
typedef struct Stack
{
STDataType* arr; //数组
int top; //栈的有效元素个数
int capacity; //栈的总大小
}ST; //取别名,方便下面代码的书写
//初始化
void StackInit(ST* ps);
//摧毁
void StackDestroy(ST* ps);
//入栈 -- 栈顶入栈
void StackPush(ST* ps, STDataType x);
//判断栈是否为空
bool StackEmpty(ST* ps);
//出栈 -- 栈顶出栈
void StackPop(ST* ps);
//取栈顶元素(不出栈)
STDataType StackTop(ST* ps);
//获取栈中有效元素个数
int StackSize(ST* ps);
Stack.c文件中的代码:
#include"Stack.h"
//初始化
void StackInit(ST* ps)
{
ps->arr = NULL; //先将数组指针设置为NULL
ps->top = ps->capacity = 0; //都是0
}
//入栈 -- 栈顶入栈
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity) //空间不够先增容
{
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity; //以二倍方式扩容
STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail!");
exit(1);
}
ps->arr = tmp; //将扩容后的地址给Stack的arr
ps->capacity = newCapacity; //修改空间大小;
}
ps->arr[ps->top++] = x; //赋值
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//出栈 -- 栈顶出栈
void StackPop(ST* ps)
{
assert(!StackEmpty(ps));
--ps->top; //将栈顶减一
}
//取栈顶元素(不出栈)
STDataType StackTop(ST* ps)
{
assert(!StackEmpty(ps));
return ps->arr[ps->top - 1];
}
//获取栈中有效元素个数
int StackSize(ST* ps)
{
return ps->top;
}
//摧毁
void StackDestory(ST* ps)
{
if (ps->arr)
free(ps->arr);
ps->arr = NULL;
ps->top = ps->capacity = 0;
}
test.c文件中的代码:
#include"Stack.h"
int main()
{
ST st;
StackInit(&st);
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
StackPush(&st, 4);
StackPop(&st);
StackPop(&st);
int size = StackSize(&st);
printf("%d ", size);
while (!StackEmpty(&st))
{
StackPop(&st);
size = StackSize(&st);
printf("%d ", size);
}
return 0;
}