using namespace std;
typedef struct LNode {
int data;
struct LNode *next;
} LNode, *List;
int n, m;
char st = 'A';
void InitList(List &L)
{
L = new LNode;
L->next = NULL;
}
void ListInput(List &L, int n)
{
int i;
List p, r;
r = L;
char filename[20] = { 0 };
cout << "请输入顺序表L" << st << "的数据文件名称(文件名+“.txt”,如List" << st << ".txt):" << endl;
++st;
gets(filename);
fstream file;
file.open(filename);
if (!file) {
cout << "未找到相关文件,无法打开!" << endl;
exit(ERROR);
}
while (!file.eof()) {
p = new LNode;
file >> p->data;
p->next = NULL;
r->next = p;
r = p;
n++;
}
file.close();
}
bool LocateElem(List L, int e)
{
List p;
p = L->next;
while (p != NULL) {
if (p->data == e)
return true;
p = p->next;
}
return false;
}
void ListInsert(List &L, int e)
{
List p;
p = new LNode;
p->data = e;
p->next = L->next;
L->next = p;
}
void ListOutput(List L)
{
List p;
p = L->next;
while (p != NULL) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
void unionList(List &LA, List LB)
{
int e;
List p;
p = LB->next;
while (p != NULL) {
e = p->data;
if (!LocateElem(LA, e))
ListInsert(LA, e);
p = p->next;
}
}
int main() {
List LA, LB;
InitList(LA);
InitList(LB);
ListInput(LA, n);
ListInput(LB, m);
unionList(LA, LB);
cout << "LA和LB合并后的集合为:\n";
ListOutput(LA);
return 0;
}