编写算法,创建初始化容量为LIST_INIT_SIZE的顺序表T,并实现插入、删除、遍历操作。本题目给出部分代码,请补全内容。
1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开
2、输入2,表示要实现删除操作,紧跟着要输入删除的位置
3、输入3,表示要输出顺序表的所有元素
4、输入0,表示程序结束
题目:
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList &L)
{
// 算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
// 请补全代码
}
int Load_Sq(SqList &L)
{
// 输出顺序表中的所有元素
int i;
if(_________________________) printf("The List is empty!"); // 请填空
else
{
printf("The List is: ");
for(_________________________) printf("%d ",_________________________); // 请填空
}
printf("\n");
return OK;
}
int ListInsert_Sq(SqList &L,int i,int e)
{
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
// i的合法值为1≤i≤L.length +1
// 请补全代码
}
int ListDelete_Sq(SqList &L,int i, int &e)
{
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
// 请补全代码
}
int main()
{
SqList T;
int a, i;
ElemType e, x;
if(_________________________) // 判断顺序表是否创建成功
{
printf("A Sequence List Has Created.\n");
}
while(1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d%d",&i,&x);
if(_________________________) printf("Insert Error!\n"); // 执行插入函数,根据返回值判断i值是否合法
else printf("The Element %d is Successfully Inserted!\n", x);
break;
case 2: scanf("%d",&i);
if(_________________________) printf("Delete Error!\n"); // 执行删除函数,根据返回值判断i值是否合法
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: Load_Sq(T);
break;
case 0: return 1;
}
}
}
这份代码哪里有错?
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
typedef struct
{
int* elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList& L)
{
// 算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
// 请补全代码
L.length = 0;
L.elem = new ElemType[LIST_INIT_SIZE];
if (!L.elem) return(ERROR);
return OK;
}
int Load_Sq(SqList& L)
{
// 输出顺序表中的所有元素
int i;
if (L.length == 0) printf("The List is empty!"); // 请填空
else
{
printf("The List is: ");
for (i = 0; i < L.length;i++) printf("%d ",L.elem[i]); // 请填空
}
printf("\n");
return OK;
}
int ListInsert_Sq(SqList& L, int i, int e)
{
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
// i的合法值为1≤i≤L.length +1
// 请补全代码
int j = 0;
if (i<1 || i>L.length) return ERROR;
else
{
for (j = L.length - 1; j >= i - 1; j--)
{
L.elem[j + 1] = L.elem[j];
}
L.elem[j] = e;
L.length++;
return OK;
}
}
int ListDelete_Sq(SqList& L, int i, int& e)
{
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
// 请补全代码
if (i<1 || i>L.length)
return ERROR;
else
{
for (; i <= L.length-1; i++)
{
L.elem[i] = L.elem[i + 1];
}
return e;
}
}
int main()
{
SqList T;
int a, i;
ElemType e, x;
if (InitList_Sq(T)) // 判断顺序表是否创建成功
{
printf("A Sequence List Has Created.\n");
}
while (1)
{
printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
scanf("%d", &a);
switch (a)
{
case 1: scanf("%d%d", &i, &x);
if (ListInsert_Sq( T, i, x)==ERROR) printf("Insert Error!\n"); // 执行插入函数,根据返回值判断i值是否合法
else printf("The Element %d is Successfully Inserted!\n", x);
break;
case 2: scanf("%d", &i);
if (ListDelete_Sq(T,i,e)==ERROR) printf("Delete Error!\n"); // 执行删除函数,根据返回值判断i值是否合法
else printf("The Element %d is Successfully Deleted!\n", e);
break;
case 3: Load_Sq(T);
break;
case 0: return 1;
}
}
}
```标准输入数据:
1
2 1
1
1 5
2
2
2
1
1
1 5
1
1 8
1
1 9
3
2
3
2
2
2
1
3
0
标准输出答案:
1|A Sequence List Has Created.
2|1:Insert element
3|2:Delete element
4|3:Load all elements
5|0:Exit
6|Please choose:
7|Insert Error!
8|1:Insert element
9|2:Delete element
10|3:Load all elements
11|0:Exit
12|Please choose:
13|The Element 5 is Successfully Inserted!
14|1:Insert element
15|2:Delete element
16|3:Load all elements
17|0:Exit
18|Please choose:
19|Delete Error!
20|1:Insert element
21|2:Delete element
22|3:Load all elements
23|0:Exit
24|Please choose:
25|The Element 5 is Successfully Deleted!
26|1:Insert element
27|2:Delete element
28|3:Load all elements
29|0:Exit
30|Please choose:
31|The Element 5 is Successfully Inserted!
32|1:Insert element
33|2:Delete element
34|3:Load all elements
35|0:Exit
36|Please choose:
37|The Element 8 is Successfully Inserted!
38|1:Insert element
39|2:Delete element
40|3:Load all elements
41|0:Exit
42|Please choose:
43|The Element 9 is Successfully Inserted!
44|1:Insert element
45|2:Delete element
46|3:Load all elements
47|0:Exit
48|Please choose:
49|The List is: 9 8 5
50|1:Insert element
51|2:Delete element
52|3:Load all elements
53|0:Exit
54|Please choose:
55|The Element 5 is Successfully Deleted!
56|1:Insert element
57|2:Delete element
58|3:Load all elements
59|0:Exit
60|Please choose:
61|The Element 8 is Successfully Deleted!
62|1:Insert element
63|2:Delete elemen
t
64|3:Load all elements
65|0:Exit
66|Please choose:
67|The Element 9 is Successfully Deleted!
68|1:Insert element
69|2:Delete element
70|3:Load all elements
71|0:Exit
72|Please choose:
73|The List is empty!
74|1:Insert element
75|2:Delete element
76|3:Load all elements
77|0:Exit
78|Please choose:
你的错误输出结果:
1|A Sequence List Has Created.
2|1:Insert element
3|2:Delete element
4|3:Load all elements
5|0:Exit
6|Please choose:
7|Insert Error!
8|1:Insert element
9|2:Delete element
10|3:Load all elements
11|0:Exit
12|Please choose:
13|Insert Error!
14|1:Insert element
15|2:Delete element
16|3:Load all elements
17|0:Exit
18|Please choose:
19|Delete Error!
20|1:Insert element
21|2:Delete element
22|3:Load all elements
23|0:Exit
24|Please choose:
25|Delete Error!
26|1:Insert element
27|2:Delete element
28|3:Load all elements
29|0:Exit
30|Please choose:
31|Insert Error!
32|1:Insert element
33|2:Delete element
34|3:Load all elements
35|0:Exit
36|Please choose:
37|Insert Error!
38|1:Insert element
39|2:Delete element
40|3:Load all elements
41|0:Exit
42|Please choose:
43|Insert Error!
44|1:Insert element
45|2:Delete element
46|3:Load all elements
47|0:Exit
48|Please choose:
49|The List is empty!
50|1:Insert element
51|2:Delete element
52|3:Load all elements
53|0:Exit
54|Please choose:
55|Delete Error!
56|1:Insert element
57|2:Delete element
58|3:Load all elements
59|0:Exit
60|Please choose:
61|Delete Error!
62|1:Insert element
63|2:Delete element
64|3:Load all elements
65|0:Exit
66|Please choose:
67|Delete Error!
68|1:Insert element
69|2:Delete element
70|3:Load all elements
71|0:Exit
72|Please choose:
73|The List is empty!
74|1:Insert element
75|2:Delete element
76|3:Load all elements
77|0:Exit
78|Please choose: