#includetypedef
struct
LNode//
定义结构体类型指针
{
char
data;
struct
LNode*next;
}*pointer;
void
readdata(pointer
head)//
定义输入集合函数
{
pointer
p;
char
tmp;
scanf("%c",&tmp);
while(tmp!='\n')
{
p=(pointer)malloc(sizeof(struct
LNode));
p->data=tmp;
p->next=head->next;
head->next=p;
scanf("%c",&tmp);
}
}
void
pop(pointer head)//定义输出集合函数
{
pointer
p;
p=head->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
void
or(pointer head1,pointer head2,pointer head3)//定义集合的交集函数
{
pointer
p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{
p2=head2->next;
while((p2!=NULL)&&(p2->data!=p1->data))
p2=p2->next;
if((p2!=NULL)&&(p2->data==p1->data))
{
p3=(pointer)malloc(sizeof(structLNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void main()//主函数
{
int x;
printf("(输入数据,按回车键结束\n");
pointerhead1,head2,head3;
head1=(pointer)malloc(sizeof(structLNode));
head1->next=NULL;
head2=(pointer)malloc(sizeof(structLNode));
head2->next=NULL;
head3=(pointer)malloc(sizeof(structLNode));
head3->next=NULL;printf("请输入集合A:\n");
readdata(head1);//调用输入集合函数
printf("请输入集合B:\n");
readdata(head2);//调用输入集合函数A:
printf("1.交集2.结束\n");
do
{
printf("请选择序号\n");
scanf("%d",&x);
switch(x)
{
case1:
or(head1,head2,head3);//调用交集函数
printf("集合A为:");
pop(head1);
printf("集合B为:");
pop(head2);
printf("两集合的交集C=A∩B:");
pop(head3);
head3->next=NULL;
break;
case2:
break;
default:
gotoA;
}
}while(x!=2);
}