This repository was archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (94 loc) · 2.48 KB
/
main.cpp
File metadata and controls
112 lines (94 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* 代码说明
* 代码由 筱锋xiao_lfeng 编写
* 其开发者由此互联网ICP备案作者编写:粤ICP备2022014822号
*/
#include <cstdio>
#include <cstdlib>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} ArrayList;
// 初始化线性表
void InitList(ArrayList &L) {
L.length = 0;
}
// 销毁线性表
void DestroyList(ArrayList &L) {}
// 按顺序插入线性表
// 用来输入一个递增有序线性表(由键盘输入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) {
return L.length;
}
// 用来找到数值为index元素的位置,返回插入位置的逻辑序号(逻辑序号从1开始)
int PositionList(ArrayList &L, int index) {
if (index > L.length || index <= 0) {
printf("[WARNING] 您所访问的数据不存在");
return -999;
}
return L.data[index - 1];
}
// 插入单个数据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) {
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);
// 返回第5个下标位置
int info_data = 5;
printf("[INFO] 返回下标第 %d 个下标的数据:%d\n",info_data, PositionList(list,info_data));
DestroyList(list);
system("pause");
return 0;
}