1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-04-02 18:18:24 +08:00

feat: 修复文档,新增算法代码

This commit is contained in:
妹妹下雨回不去
2023-03-02 17:39:42 +08:00
parent 7bd3072ee1
commit 8262d7ab42
58 changed files with 1725 additions and 1679 deletions

81
code/ds/SqStack.cpp Normal file
View File

@@ -0,0 +1,81 @@
/*
* @Description: 顺序栈的相关操作
* @Version: Beta1.0
* @Author: 【B站&公众号】Rong姐姐好可爱
* @Date: 2020-03-07 11:15:04
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
* @LastEditTime: 2021-03-13 12:30:18
*/
// 定义栈中元素的最大个数
# define MaxSize 50
// 结构体定义
typedef struct{
ElemType data[MaxSize]; // 存放栈中元素
int top; // 栈顶指针
}SqStack;
// 初始化
void InitStack(&S){
// 栈顶指针-1
s.top=-1;
}
// 栈空判断
bool StackEmpty(S){
if(S.top==-1){
// 栈空
return true;
}else{
// 栈非空
return false;
}
}
// 进栈
bool Push(SqStack &S,ElemType x){
if(S.top==MaxSize-1){
// 栈满返回false元素无法进行进栈操作
return false;
}else{
// 可进栈,栈顶指针+1再元素入栈
S.data[++S.top]=x;
// 入栈成功
return true;
}
}
// 出栈
bool Pop(SqStack &S,ElemType &x){
if(S.top==-1){
// 栈空无栈顶元素可出栈返回false
return false;
}else{
// 栈非空,先元素出栈,再进行指针-1
x=S.data[S.top--];
// 出栈成功返回true
return true;
}
}
// 读(获取)栈顶元素
bool GetTop(SqStack S,ElemType &x){
if(S.top==-1){
// 栈空,无栈顶元素,返回false
return false;
}else{
// 通过栈顶指针获取栈顶元素赋值给变量x
x=S.data[S.top];
// 读取栈顶元素成功返回true
return true;
}
}