程序填空题:求两个线性表的并集(顺序存储)
这是程序填空题模板。题目要求写在这里。例如:本题目要求打印“Hello World!”。
```
#include
using namespace std;
#define MAXSIZE 100
#define ERROR 0
typedef struct {
int *elem;
int length;
} List;
void InitList(List &L)
{
L.elem = new int[MAXSIZE];
L.length = 0;
}
void ListInput(List &L, int n)
{
int i = 0;
while (i cin >> L.elem[i];
i++;
}
L.length = i;
}
int ListLength(List L)
{
return L.length;
}
void GetElem(List L, int i, int &e)
{
e = L.elem[i - 1];
}
bool LocateElem(List L, int e)
{
int i;
for (i = 0; i < L.length; i++)
if (e == L.elem[i])
return true;
return false;
}
void ListInsert(List &L, int e)
{
L.elem[L.length] = e;
L.length++;
}
void ListOutput(List L)
{
int i;
for (i = 0; i < L.length; i++)
cout << L.elem[i] << " ";
cout << endl;
}
void unionList(List &LA, List LB)
{
int LA_len, LB_len, i, e;
LA_len = ListLength(LA);
LB_len = ListLength(LB);
for (i = 1; i <= LB_len; i++) {
@@[GetElem(LB, i, e); ](2)
if (@@[!LocateElem(LA, e)](2))
@@[ListInsert(LA, e);](2)
}
}
int main() {
List LA, LB;
int n, m;
cin >> n >> m;
InitList(LA);
InitList(LB);
ListInput(LA, n);
ListInput(LB, m);
unionList(LA, LB);
ListOutput(LA);
return 0;
}
```
答案:
第1空:GetElem(LB, i, e);
第2空:!LocateElem(LA, e)
第3空:ListInsert(LA, e);
```
#include
using namespace std;
#define MAXSIZE 100
#define ERROR 0
typedef struct {
int *elem;
int length;
} List;
void InitList(List &L)
{
L.elem = new int[MAXSIZE];
L.length = 0;
}
void ListInput(List &L, int n)
{
int i = 0;
while (i
i++;
}
L.length = i;
}
int ListLength(List L)
{
return L.length;
}
void GetElem(List L, int i, int &e)
{
e = L.elem[i - 1];
}
bool LocateElem(List L, int e)
{
int i;
for (i = 0; i < L.length; i++)
if (e == L.elem[i])
return true;
return false;
}
void ListInsert(List &L, int e)
{
L.elem[L.length] = e;
L.length++;
}
void ListOutput(List L)
{
int i;
for (i = 0; i < L.length; i++)
cout << L.elem[i] << " ";
cout << endl;
}
void unionList(List &LA, List LB)
{
int LA_len, LB_len, i, e;
LA_len = ListLength(LA);
LB_len = ListLength(LB);
for (i = 1; i <= LB_len; i++) {
@@[GetElem(LB, i, e); ](2)
if (@@[!LocateElem(LA, e)](2))
@@[ListInsert(LA, e);](2)
}
}
int main() {
List LA, LB;
int n, m;
cin >> n >> m;
InitList(LA);
InitList(LB);
ListInput(LA, n);
ListInput(LB, m);
unionList(LA, LB);
ListOutput(LA);
return 0;
}
```
答案:
第1空:GetElem(LB, i, e);
第2空:!LocateElem(LA, e)
第3空:ListInsert(LA, e);