㈠ 请问如何用c语言代码实现两个集合的交,并,相对补,对称差的运算并判断两个集合是否相等
先取到两个集合的数组或是指针,循环遍历各个元素,至于二者的交、并、补可以用与、或、非来实现,实现方式大概就是这样。
㈡ 数据结构 用c语言写的 集合的并、交和差运算的程序
可以用二个一维数组,
再用两个for循环来判断结果:交,并,差
在for循环中,用一个if来判断一下,是不是a[0]==b[j],只要有相等的,就令之放在c[0]
这就是交集!!
并集就好求吧,
只要令c[i]=a[i],再来一个就是c[i+j+1]=b[j](因为我这里是考虑j=0开始的,然后自加差就是在交上改动一下就可以了,只要是a[0]!=b[j],就把它放到c[]这个数组里面去~!!!!
1:并集的程序。
求集合LA和集合LB的并集
#define NULL 0
struct JD
{ int data;
struct JD *next;
};
int find(int number,struct JD *h)
{ while(h->data)
{ if(h->data!=number)
{ h=h->next;
continue;
}
else
return 0;
}
return 1;
}
struct JD * make()
{ struct JD *h=NULL,*p=NULL;
int number,tf;
h=(struct JD *)malloc(sizeof(struct JD));
scanf("%d",&h->data);
p=h;
while(p->data)
{ p->next=(struct JD *)malloc(sizeof(struct JD));
p=p->next;
p->data=0;
scanf("%d",&number);
tf=find(number,h);
if(tf)
p->data=number;
else
continue;
}
return h;
}
void print(struct JD *h)
{ while(h->data)
{ printf("%d ",h->data);
h=h->next;
}
}
struct JD * change(struct JD *la,struct JD *lb)
{ struct JD *h,*p,*s,*q;
int number,tf;
p=lb;
while(p->data)
{ number=p->data;
tf=find(number,la);
p=p->next;
if(tf)
{ s=(struct JD *)malloc(sizeof(struct JD));
s->data=number;
s->next=la;
la=s;
}
else
continue;
}
return la;
}
void del(struct JD *h)
{ struct JD *p=h->next;
while(h->data)
{ free(h);
h=p;
p=p->next;
}
free(h);
}
main()
{ struct JD *la,*lb;
printf("\n\nGive the number to LA :\n\n");
la=make();
printf("\nLA is: ");
print(la);
printf("\n\nGive the number to LB :\n\n");
lb=make();
printf("\nLB is: ");
print(lb);
la=change(la,lb);
printf("\n\n\nThe new LA=LA||LB is: ");
print(la);
del(la);
del(lb);
printf("\n\n\nPass any key to exit...!\n");
getch();
}
********** 程序运行结果 **********
Give the number to LA :
1↓
2↓
3↓
5↓
0↓
LA is: 1 2 3 5
Give the number to LB :
6↓
7↓
3↓
2↓
9↓
0↓
LB is: 6 7 3 2 9
The new LA=LA||LB is: 9 7 6 1 2 3 5
--------------------------------------------------
Pass any key to exit...!
㈢ 单链表表示的集合交,并,差运算,设计采用定义集合,用集合运算表达式求值的方式进行。C语言实现。
#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(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);
}
㈣ 实验、集合的交、并差 用c语言
#include"stdio.h"
intinput(intd[],intn)
{
inti;
//n=0;
do
{
scanf("%d",d+n);
n+=1;
}while(d[n-1]>=0);
d[n-1]='