代码实现
#include
#include
#include
#define InitSize 3
#define ElemType int
typedef struct {
ElemType *data;
int MaxSize, length;
} SeqList;
bool Empty(SeqList list);
void InitList(SeqList *list) {
list->data = (ElemType *) malloc(InitSize * sizeof(ElemType));
list->MaxSize = InitSize;
list->length = 0;
}
bool MultipleList(SeqList *list) {
if(list->data != NULL){
list->data = (ElemType *) realloc(list->data, list->MaxSize * sizeof(ElemType) * 2);
list->MaxSize = list->MaxSize * 2;
return true;
}
printf("扩容失败!\n");
return false;
}
int Length(SeqList list) {
return list.length;
}
void AddElem(SeqList *list, ElemType Elem) {
if (list->length == list->MaxSize) {
if(!MultipleList(list)){
printf("扩容失败,线性表已被销毁!\n");
return;
}
}
list->data[list->length] = Elem;
list->length++;
}
int LocateElem(SeqList list, ElemType Elem) {
if (Empty(list)) {
return -1;
}
for (int i = 0; i < list.length; i++) {
if (list.data[i] == Elem)
return i;
}
return -1;
}
bool GetElem(SeqList list, int index, ElemType *Elem) {
if (index >= 0 && index < list.length) {
*Elem = list.data[index];
return true;
}
printf("获取失败!\n");
return false;
}
bool ListInsert(SeqList *list, int index, ElemType Elem) {
if (list->length == list->MaxSize) {
MultipleList(list);
}
if (index >= 0 && index < list->length) {
for (int i = list->length - 2; i >= index; i--) {
list->data[i + 1] = list->data[i];
}
list->data[index] = Elem;
list->length++;
return true;
}
return false;
}
bool ListDelete(SeqList *list, int index) {
if (index >= 0 && index < list->length) {
for (int i = index; i < list->length - 1; i++) {
list->data[i] = list->data[i+1];
}
list->length--;
return true;
}
return false;
}
void DestroyList(SeqList *list){
if(list->data != NULL) {
free(list->data);
list->data = NULL;
}
list->length = 0;
list->MaxSize = 0;
}
void PrintList(SeqList list) {
if (Empty(list)) {
printf("线性表为空!\n");
return;
}
for (int i = 0; i < list.length; i++) {
printf("%d -> %d\n", i, list.data[i]);
}
}
bool Empty(SeqList list) {
if (list.length == 0)
return true;
return false;
}
int main() {
SeqList a;
ElemType Elem;
int index, choice;
InitList(&a);
while (1) {
printf("\n菜单\n"
"1.查看线性表\n"
"2.获取线性表长度\n"
"3.添加线性表元素\n"
"4.按值查找操作\n"
"5.按位查找操作\n"
"6.插入操作\n"
"7.删除操作\n"
"8.销毁线性表\n"
"9.退出程序\n"
"10.获取线性表最大长度(测试)\n"
"请输入你要进行的操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
PrintList(a);
break;
case 2:
printf("当前线性表长度为%d\n", Length(a));
break;
case 3:
printf("请输入要添加的数据数值:");
scanf("%d", &Elem);
AddElem(&a, Elem);
break;
case 4:
printf("请输入要查找的数据元素值:");
scanf("%d", &Elem);
index = LocateElem(a, Elem);
if (index == -1) {
printf("元素查找失败!\n");
break;
} else {
printf("元素%d对应的数组下标为%d\n", Elem, index);
break;
}
case 5:
printf("请输入要查找的数据元素的数组下标:");
scanf("%d", &index);
if (GetElem(a, index, &Elem)) {
printf("数组下标为%d的元素为%d\n", index, Elem);
break;
} else {
printf("查找失败!\n");
break;
}
case 6:
printf("请输入要插入的元素的位置(数组下标):");
scanf("%d", &index);
printf("请输入要插入的元素的数据值:");
scanf("%d", &Elem);
if (ListInsert(&a, index, Elem)) {
printf("插入成功!\n");
break;
} else {
printf("插入失败!输入的数组下标不合法\n");
break;
}
case 7:
printf("请输入要删除的元素的数组下标:");
scanf("%d",&index);
if(ListDelete(&a,index)){
printf("元素删除成功!\n");
break;
}
else{
printf("元素删除失败!输入的下标不合法\n");
break;
}
case 8:
DestroyList(&a);
break;
case 9:
exit(0);
case 10:
printf("当前线性表最大长度为%d\n",a.MaxSize);
break;
default:
printf("输入的操作有误,请重新输入!\n");
break;
}
}
}

- 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
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262