持新加坡签证免签国家:数据结构 用c语言写的 集合的并、交和差运算的程序

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/12 19:33:46
基本要求:(1)集合的元素限定为小写字母字符〔‘a’..'z'〕.
(2)演示程序以用户和计算机的对话方式执行。
实现提示:
以有序链表表示集合。

以下程序由标准C实现,并经严格测试。程序通过单链表存储集合

#include<stdio.h>
#include<stdlib.h>

typedef struct pointer{
char dat;
struct pointer *link;
} pointer;

void readdata(pointer *head){ //读集合
pointer *p;
char tmp;
printf("input data ('0' for end):");
scanf("%c",&tmp);
while(tmp!='0')
{
if((tmp<'a')||(tmp>'z'))
{
printf("输入错误!必须为小写字母!\n");
return;
}
p=(pointer *)malloc(sizeof(struct pointer));
p->dat=tmp;
p->link=head->link;
head->link=p;
scanf("%c",&tmp);
}
}

void disp(pointer *head){ //显示集合数据
pointer *p;
p=head->link;
while(p!=NULL)
{
printf("%c ",p->dat);
p=p->link;
}
printf("\n");
}

void bing(pointer *head1,pointer *head2, pointer *head3){ //计算集合1与集合2的并
pointer *p1,*p2,*p3;
p1=head1->link;
while(p1!=NULL)
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p1->dat;
p3->link=head3->link;
head3->link=p3;
p1=p1->link;
}
p2=head2->link;
while(p2!=NULL)
{
p1=head1->link;
while((p1!=NULL)&&(p1->dat!=p2->dat))
p1=p1->link;
if(p1==NULL)
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p2->dat;
p3->link=head3->link;
head3->link=p3;
}
p2=p2->link;
}
}

void jiao(pointer *head1,pointer *head2, pointer *head3){ //计算集合1与集合2的交
pointer *p1,*p2,*p3;
p1=head1->link;
while(p1!=NULL)
{
p2=head2->link;
while((p2!=NULL)&&(p2->dat!=p1->dat))
p2=p2->link;
if((p2!=NULL)&&(p2->dat=p1->dat))
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p1->dat;
p3->link=head3->link;
head3->link=p3;
}
p1=p1->link;
}
}

void cha(pointer *head1,pointer *head2, pointer *head3){ //计算集合1与集合2的差
pointer *p1,*p2,*p3;
p1=head1->link;
while(p1!=NULL)
{
p2=head2->link;
while((p2!=NULL)&&(p2->dat!=p1->dat))
p2=p2->link;
if(p2==NULL)
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p1->dat;
p3->link=head3->link;
head3->link=p3;
}
p1=p1->link;
}
}

main(){
pointer *head1,*head2,*head3;
head1=(pointer *)malloc(sizeof(struct pointer));
head1->link=NULL;
head2=(pointer *)malloc(sizeof(struct pointer));
head2->link=NULL;
head3=(pointer *)malloc(sizeof(struct pointer));
head3->link=NULL;
printf("输入集合1:\n");
readdata(head1);
printf("输入集合2:\n");
readdata(head2);
printf("集合1为:\n");
disp(head1);
printf("集合2为:\n");
disp(head2);
printf("集合1与集合2的并为:\n");
bing(head1,head2,head3);
disp(head3);
head3->link=NULL;
printf("集合1与集合2的交为:\n");
jiao(head1,head2,head3);
disp(head3);
head3->link=NULL;
printf("集合1与集合2的差为:\n");
cha(head1,head2,head3);
disp(head3);
}

测试用例为(0表示集合输入结束):
fdsa0
savc0

可以用二个一维数组,
再用两个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...!

p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p1->dat;
p3->link=head3->link;
head3->link=p3;
}
p1=p1->link;
}
}

void cha(pointer *head1,pointer *head2, pointer *head3){ //计算集合1与集合2的差
pointer *p1,*p2,*p3;
p1=head1->link;
while(p1!=NULL)

我也没学到#75

我还没学呢~