求A 并(B交C)
#include<bits/stdc++.h>
using namespace std;
struct LNode
{
int data;
struct LNode *next;
};
void creathead(LNode *&L,int a[],int n)///尾插
{
LNode *r,*s;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
r=L;
for(int i=0; i<n; i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=r->next;
}
r->next=NULL;
}
void Union(LNode *A,LNode *B)///求并集
{
int book[1000]= {0};///避免求并集时有重复元素进行标记
LNode *p,*q,*r;
p=A->next;
q=B->next;
r=A;
while(p && q)
{
if(p->data < q->data)
{
if(book[p->data]==0)
{
book[p->data]=1;
r->next=p;
r=p;
}
p=p->next;
}
else if(p->data > q->data)
{
if(book[q->data]==0)
{
book[q->data]=1;
r->next=q;
r=q;
}
q=q->next;
}
else
{
if(book[p->data]==0)
{
book[p->data]=1;
r->next=p;
r=p;
}
p=p->next;
q=q->next;
}
}
if(q!=NULL)
p=q;
while(p)
{
if(book[p->data]==0)
{
book[p->data]=1;
r->next=p;
r=p;
}
p=p->next;
}
r->next=NULL;
printf("并集结果:\n");
while(A->next)
{
printf("&&&%d ",A->next->data);
A=A->next;
}
printf("\n");
}
void interset(LNode *A,LNode *B)///求交集
{
int book[1000]= {0};
LNode *p,*q,*r;
p=A->next;
q=B->next;
A->next=NULL;
r=A;
while(p && q)
{
if(p->data < q->data)
p=p->next;
else if(q->data < p->data)
q=q->next;
else if(p->data == q->data)
{
if(book[p->data]==0)
{
book[p->data]=1;
r->next=p;
r=p;
}
p=p->next;
q=q->next;
}
}
r->next=NULL;
printf("交集结果:\n");
while(A->next)
{
printf("***%d ",A->next->data);
A=A->next;
}
printf("\n");
}
void solve(LNode *A,LNode *B,LNode *C)
{
interset(B,C);
Union(A,B);
while(A->next)
{
printf("%d ",A->next->data);
A=A->next;
}
printf("\n");
}
int main()
{
LNode *B,*C,*A;
int a[10000],b[10000],c[10000];
int n1,n2,n3;
scanf("%d",&n1);
for(int i=0; i<n1; i++)
scanf("%d",&a[i]);
creathead(A,a,n1);
scanf("%d",&n2);
for(int i=0; i<n2; i++)
scanf("%d",&b[i]);
creathead(B,b,n2);
scanf("%d",&n3);
for(int i=0; i<n3; i++)
scanf("%d",&c[i]);
creathead(C,c,n3);
solve(A,B,C);
return 0;
}
/***
6
1 2 2 2 3 4
5
1 2 3 4 6
6
1 2 4 5 6 9
***2
&&&1 &&&2 &&&2 &&&2 &&&3 &&&4
1 2 2 2 3 4
***/