1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-04-13 18:00:58 +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

80
code/ds/SqList.cpp Normal file
View File

@@ -0,0 +1,80 @@
/*
* @Description: 顺序表的基础操作
* @Version: Beta1.0
* @Author: 【B站&公众号】Rong姐姐好可爱
* @Date: 2021-02-23 07:48:26
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
* @LastEditTime: 2021-02-23 07:48:26
*/
// 基础结构体
define MaxSize 50;
typedef struct{
ElemType data[MaxSize]; // ElemType 代表元素类型 int、string.....
int length;
}SqList
bool ListInsert(SqList &L, int i, ElemType e){
// i非法 i=1 表头 i=L.length+1 表尾巴
if(i<1||i>L.length+1){
return false;
}
// 存储空间满,无法插入
if(L.length >= MaxSize){
return false;
}
// 遍历,将位置元素往后移动,注意从后往前循环,避免值被覆盖
for(int j=L.length; j>=i;j--){
L.data[j]=L.data[j-1];
}
// 此时表L中的第i个元素和第i+1元素素值一样将新元素存入i位置即可
// 第i个元素对应的位置角标为i-1
L.data[i-1]=e;
// 表长度加1
L.length++;
// 返回插入成功
return true;
}
bool ListDelete(SqList &L, int i, ElemType &e){
// i非法 i=1 表头 i=L.length+1 表尾巴
if(i<1||i>L.length+1){
return false;
}
// 存储空间满,无法插入
if(L.length >= MaxSize){
return false;
}
// 引用变量e赋值
e=L.data[i-1]
// 遍历第i个元素后面的往前移动
for(int j=i; j<=L.length;j++){
// 从第i个元素开始角标从i-1开始
L.data[j-1]=L.data[j];
}
// 此时表L中的表尾元素和倒数第二个元素值一样将表的长度-1
// 表长度减1
L.length--;
// 返回删除成功
return true;
}