#include<stdio.h>
#include<stdlib.h>
typedef 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(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void main()//主函数
{
int x;
printf("(输入数据,按回车键结束\n");
pointer head1,head2,head3;
head1=(pointer)malloc(sizeof(struct LNode));
head1->next=NULL;
head2=(pointer)malloc(sizeof(struct LNode));
head2->next=NULL;
head3=(pointer)malloc(sizeof(struct LNode));
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)
{
case 1:
or(head1,head2,head3);
printf("集合A为:");pop(head1);
printf("集合B为:");pop(head2);
printf("两集合的交集C=A∩B:");
pop(head3);
head3->next=NULL;
break;
case 2:break;
default:goto A;
}
}while(x!=2);
}
head3->next=NULL;
break;
case 2:break;
default:goto A;
}
}while(x!=2);
}
#include<stdlib.h>
typedef 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(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void main()//主函数
{
int x;
printf("(输入数据,按回车键结束\n");
pointer head1,head2,head3;
head1=(pointer)malloc(sizeof(struct LNode));
head1->next=NULL;
head2=(pointer)malloc(sizeof(struct LNode));
head2->next=NULL;
head3=(pointer)malloc(sizeof(struct LNode));
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)
{
case 1:
or(head1,head2,head3);
printf("集合A为:");pop(head1);
printf("集合B为:");pop(head2);
printf("两集合的交集C=A∩B:");
pop(head3);
head3->next=NULL;
break;
case 2:break;
default:goto A;
}
}while(x!=2);
}
head3->next=NULL;
break;
case 2:break;
default:goto A;
}
}while(x!=2);
}