This repository has been archived on 2024-05-05. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ABS-Peripheral-Drive/数据结构//Stack.c
2023-10-18 18:00:04 +08:00

68 lines
1.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
*@文件:Stack.c
*@作者:‘你遇了我’
*@time:2023/10/18
*@联系:QQ:321640253
*@版本:V1.0
*@描述:顺序泛型栈
*/
#include "Stack.h"
/*
* @简介:初始化一个栈
* @参数:
* *S栈结构体指针
* Maxsize栈容器的长度
* NodeSize:栈容器每个元素的空间大小
* *container容器指针
* @返回值:无
* */
void InitStack(SqStack *S,uint16_t MaxSize,uint16_t NodeSize) {
S->container = calloc(MaxSize,NodeSize);
S->Top=-1;
S->MaxSize=MaxSize;
S->NodeSize=NodeSize;
S->size = 0;
}
/*
* @简介:进栈
* @参数:
* *S栈结构体指针
* @返回值:可进栈空间地址
* */
void *PushStack(SqStack *S) {
if(S->size>=S->MaxSize)return NULL;
S->Top++;
S->size++;
return S->container + S->Top*S->NodeSize;
}
/*
* @简介:出栈
* @参数:
* *S栈结构体指针
* @返回值:出栈元素地址空间
* */
void *PopStack(SqStack *S) {
if (S->size == 0) {
return NULL;
}
S->Top--;
S->size--;
return S->container+S->Top*S->NodeSize;
}
/*
* @简介:销毁栈
* @参数:
* *S栈结构体指针
* @返回值:无
* */
void DestroyStack(SqStack *S) {
free(S->container);
S->container = NULL;
S->Top = -1;
S->MaxSize = 0;
S->size = 0;
}