C语言求交集、并集、差集

发表时间:2018-09-13 10:06:17 -0400

 
(作者:KX)(code:c)#include <stdio.h>
#include <stdlib.h>
struct Num
{
int num;
struct Num *next;
};
void insertList(struct Num *head,int num);
void printList(struct Num *head);
int isInList(struct Num *head,int num);
struct Num *getJiao(struct Num *head1,struct Num *head2);
struct Num *getBing(struct Num *head1,struct Num *head2);
struct Num *getCha(struct Num *head1,struct Num *head2);
void freeList(struct Num *head);
int main()
{
struct Num *headA,*headB,*headJiao,*headBing,*headCha;
int num,i,n;
headA=(struct Num *)malloc(sizeof(struct Num));
headA->next=NULL;
headB=(struct Num *)malloc(sizeof(struct Num));
headB->next=NULL;
printf("输入集合A的元素个数:");
scanf("%d",&num);
printf("输入集合A的各个元素,按空格隔开:\n");
for(i=0;i<num;i++)
{
scanf("%d",&n);
insertList(headA,n);
}
printf("输入集合B的元素个数:");
scanf("%d",&num);
printf("输入集合B的各个元素,按空格隔开:\n");
for(i=0;i<num;i++)
{
scanf("%d",&n);
insertList(headB,n);
}
printf(" A = ");
printList(headA);
printf(" B = ");
printList(headB);
headJiao=getJiao(headA,headB);
printf("A∩B = ");
printList(headJiao);
headBing=getBing(headA,headB);
printf("A∪B = ");
printList(headBing);
headCha=getCha(headA,headB);
printf("A-B = ");
printList(headCha);
freeList(headA);
freeList(headB);
freeList(headJiao);
freeList(headBing);
freeList(headCha);
return 0;
}
void insertList(struct Num *head,int num)
{
struct Num *pnew;
pnew=(struct Num *)malloc(sizeof(struct Num));
pnew->next=head->next;
pnew->num=num;
head->next=pnew;
}
void printList(struct Num *head)
{
struct Num *pcur=head->next;
if(!pcur)
{
printf("Φ");
}
else
{
printf("{");
while(pcur)
{
printf("%d",pcur->num);
pcur=pcur->next;
if(pcur)
printf(",");
}
printf("}");
}
printf("\n");
}
int isInList(struct Num *head,int num)
{
struct Num *pcur=head->next;
int result=0;
while(pcur)
{
if(pcur->num==num)
{
result=1;
break;
}
pcur=pcur->next;
}
return result;
}
struct Num *getJiao(struct Num *head1,struct Num *head2)
{
struct Num *head=(struct Num *)malloc(sizeof(struct Num));
struct Num *pcur;
head->next=NULL;
pcur=head1->next;
while(pcur)
{
if(isInList(head2,pcur->num) && !isInList(head,pcur->num))
{
insertList(head,pcur->num);
}
pcur=pcur->next;
}
return head;
}
struct Num *getBing(struct Num *head1,struct Num *head2)
{
struct Num *head=(struct Num *)malloc(sizeof(struct Num));
struct Num *pcur;
head->next=NULL;
pcur=head1->next;
while(pcur)
{
insertList(head,pcur->num);
pcur=pcur->next;
}
pcur=head2->next;
while(pcur)
{
if(!isInList(head,pcur->num))
{
insertList(head,pcur->num);
}
pcur=pcur->next;
}
return head;
}
struct Num *getCha(struct Num *head1,struct Num *head2)
{
struct Num *head=(struct Num *)malloc(sizeof(struct Num));
struct Num *pcur;
head->next=NULL;
pcur=head1->next;
while(pcur)
{
if(!isInList(head2,pcur->num))
{
insertList(head,pcur->num);
}
pcur=pcur->next;
}
return head;
}
void freeList(struct Num *head)
{
struct Num *pcur1,*pcur2;
pcur1=head;
while(pcur1)
{
pcur2=pcur1->next;
free(pcur1);
pcur1=pcur2;
}
}
(/code)
Comment:
Name:

返回上级 | 返回首页