1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-04-10 14:08:47 +08:00
This commit is contained in:
mmdapl
2021-02-23 08:20:09 +08:00
parent a1f96bad77
commit c05c8ada92
3 changed files with 80 additions and 0 deletions

BIN
.DS_Store vendored

Binary file not shown.

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;
}

Binary file not shown.