原题链接:数据结构-集合union - C语言网 (dotcpp.com)
```
#include<stdio.h>
#include<malloc.h>
typedef struct Union{
int data;
struct Union* next;
}*node,Node;
//建立链表函数
node creat (int);
//取出L链表中的第i个元素
int GetElem (node, int);
//查找链表中的某元素
int LocateElem (node, int);
//将元素插入到链表的末尾
void ListInsert (node, int);
//输出链表
void output (node);
int main(){
int a,b;//两个集合中元素的个数
node head1,head2;//头节点
int e;
while (scanf("%d",&a)!=EOF){
int i;
//建立头节点
head1=(node)malloc(sizeof(Node));
head2=(node)malloc(sizeof(Node));
//创建两个集合
head1=creat(a);
scanf ("%d",&b);
head2=creat(b);
//输出两个集合
output(head1);
output(head2);
for (i=1;i<=b;i++){
//取出第二个集合中的第i个元素
e=GetElem (head2, i);
//查找第一个集合中是否存在该元素
//若不存在,将该元素插入到集合中
if (!LocateElem(head1,e))
ListInsert(head1,e);
//每一次都输出当前的集合
output(head1);
}
printf("\n");
}
return 0;
}
node creat (int n){
int i;
node h,p,q;
h=(node)malloc(sizeof(Node));
h->next=NULL;
q=h;
for (i=0;i<n;i++){
p=(node)malloc(sizeof(Node));
scanf ("%d",&(*p).data);
p->next=q->next;
q->next=p;
q=p;
}
return h;
}
int GetElem (node L, int i){
node temp0;
temp0=L;
for (int j=1;j<=i;j++){
temp0=temp0->next;
}
return temp0->data;
}
int LocateElem (node L, int e){
node temp=L;
while (temp->next !=NULL){
temp=temp->next;
if (temp->data == e)
return 1;
}
return 0;
}
void ListInsert (node L, int e){
node temp=L,p;
while (temp->next !=NULL){
temp=temp->next;
}
p=(node)malloc(sizeof(Node));
p->data=e;
p->next=temp->next;
temp->next=p;
}
void output (node L){
node temp=L;
while (temp->next !=NULL){
temp=temp->next;
printf("%d ",temp->data);
}
printf("\n");
}
```