线性表

This commit is contained in:
筱锋xiao_lfeng 2023-05-20 17:23:02 +08:00
parent 4827456057
commit 55d954ead4
3 changed files with 61 additions and 11 deletions

BIN
ArrayList.exe Normal file

Binary file not shown.

View File

@ -3,6 +3,8 @@
*/
#include <cstdio>
#include <cstdlib>
#define MAXSIZE 100
typedef struct {
@ -16,41 +18,89 @@ void InitList(ArrayList &L) {
}
// 销毁线性表
void DestroyList(ArrayList &L) { }
void DestroyList(ArrayList &L) {}
// 按顺序插入线性表
// 用来输入一个递增有序线性表由键盘输入n个整数
void PutSeqList(ArrayList &L,int n) {
void PutSeqList(ArrayList &L, int n) {
// 判断当前线性表加入n个内容是否溢出
if (L.length + n >= MAXSIZE) {
printf("[WARNING] 线性表超出限制大小,请重试");
return;
}
// 循环加入数据
int inputData;
printf("[INFO] 请输入 %d 个数据(中间用回车隔开):\n", n);
for (int i = L.length; i < L.length + n; ++i) {
printf("[INPUT] 正在输入第 %d 个数据:", i + 1);
scanf_s("%d", &inputData);
L.data[i] = inputData;
}
L.length += n;
}
int LengthList(ArrayList &L) {
// 线性表的长度
int LengthList(ArrayList L) {
return L.length + 1;
}
int PositionList(ArrayList &L,int x) {
// 用来找到数值为index元素的位置返回插入位置的逻辑序号逻辑序号从1开始
int PositionList(ArrayList &L, int index) {
if (index > L.length || index <= 0) {
printf("[WARNING] 您所访问的数据不存在");
return -999;
}
return L.data[index - 1];
}
int InsertList(ArrayList &L,int i,int e) {
// 插入单个数据e
int InsertList(ArrayList &L, int index, int e) {
if (index > L.length || index <= 0) {
printf("[WARNING] 您所访问的数据不存在");
return 0;
}
for (int i = L.length; i >= index - 1; --i) {
L.data[i] = L.data[i - 1];
}
L.data[index - 1] = e;
L.length++;
printf("[WARNING] 线性表在 %d 位置插入数据 %d\n", index, e);
return 1;
}
void OutputSeqList(ArrayList &L) {
// 输出全部数据
void OutputSeqList(ArrayList L) {
printf("[INFO] 线性表中数据为:[ ");
for (int i = 0; i < L.length - 1; ++i) {
printf("%d ", L.data[i]);
}
printf("%d", L.data[L.length - 1]);
printf(" ]\n");
}
int main() {
ArrayList list;
InitList(list);
// 插入五个数据
int size;
printf("[WAITING] 请输入需要插入多少数据:");
scanf_s("%d",&size);
PutSeqList(list, size);
// 表长
printf("[INFO] 表长为:%d\n", LengthList(list));
OutputSeqList(list);
// 在3位置后面插入指定数据0
InsertList(list, 3, 0);
// 表长
printf("[INFO] 表长为:%d\n", LengthList(list));
OutputSeqList(list);
DestroyList(list);
system("pause");
return 0;
}

BIN
参考图.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 419 KiB