数据结构 用c语言写的 集合的并、交和差运算的程序

作者&投稿:乘怪 (若有异议请与网页底部的电邮联系)
用C语言编写一个集合的交,并和差运算的程序怎么写啊?~

/*第一,你的题意不明,我只能输入两个集合了【互异性由输入保证】*/#include#includevoid main(){ char temp[60]="",str1[30]="",str2[30]="",i,j,l1,l2,ch; printf("STR1:"); gets(str1); printf("STR2:"); gets(str2); l1=strlen(str1); l2=strlen(str2); //交集 printf("
交集:
{"); for(i=0;i排序->删除相同printf("
并集:
{"); /*合并*/sprintf(temp,"%s%s",str1,str2); /*排序*/ for(i=0;itemp[j]) { char ch; ch=temp[i]; temp[i]=temp[j]; temp[j]=ch; } /*删除相同字符*/ for(i=j=1;i<l1+l2;i++) if(temp[i]!=temp[j-1]) temp[j++]=temp[i]; temp[j]='\0'; for(i=0;i<j;i++) printf("%c,",temp[i]);printf("\b}
"); //CuAprintf("
CuA:
{"); for(ch='a';ch<='z';ch++) { for(i=0;i<l1;i++) if(ch==str1[i]) goto NOT; printf("%c,",ch);NOT:if(0); } printf("\b}
"); //CuB printf("
CuB:
{"); for(ch='a';ch<='z';ch++) { for(i=0;i<l2;i++) if(ch==str2[i]) goto NOT2; printf("%c,",ch);NOT2:if(0); } printf("\b}
");}

这是求并集的算法描述,链式结构实现的,参考一下
算法描述
LNode *Merge_LinkList(LNode *La, LNode *Lb)
/* 合并以La, Lb为头结点的两个有序单链表 */
{ LNode*Lc, *pa , *pb, *pc, *ptr ;
Lc=La; pc=La ; pa=La->next ; pb=Lb->next ;
while (pa!=NULL && pb!=NULL)
{ if (pa->datadata)
{ pc->next=pa ; pc=pa ; pa=pa->next ; }
/* 将pa所指的结点合并,pa指向下一个结点 */
if (pa->data>pb->data)
{ pc->next=pb; pc=pb ; pb=pb->next ; }
/* 将pa所指的结点合并,pa指向下一个结点 */
if (pa->data==pb->data)
{ pc->next=pa ; pc=pa ; pa=pa->next ;
ptr=pb; pb=pb->next; free(ptr); }
/* 将pa所指的结点合并,pb所指结点删除 */
}
if (pa!=NULL) pc->next=pa ;
else pc->next=pb; /*将剩余的结点链上*/
free(Lb) ;
return(Lc) ;
}

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

以下程序由标准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

我还没学呢~

ao


天峻县19825675577: 用C语言合并两个集合 -
子丰萱鼻渊: 楼主可以参考严蔚敏的《数据结构》,清华出版社的,第二章有讲合并集合,有算法,填一下就可以用,没有学线性表可以参考算法思想.

天峻县19825675577: 数据结构 用c语言写的 集合的并、交和差运算的程序 -
子丰萱鼻渊: 安装雅虎助手可以

天峻县19825675577: 数据结构(C语言):顺序表的合并 -
子丰萱鼻渊: 程序没什么大的毛病..只是一些编写代码时的小疏忽 #include#include #define ok 1 #define error 0 #define list_int_size 10 #define elemtype int typedef struct{ elemtype *elem; int length; int listsize; }sqlist; int init_sqlist(sqlist *la) { (*la).elem=(...

天峻县19825675577: 数据结构,链表的并集操作. -
子丰萱鼻渊: #include<malloc.h> #include<stdio.h> #include<stdlib.h> typedef struct Lnode{int data;struct Lnode *next; }Lnode,*Linklist;void initlist(Linklist L) {L->next = NULL; //将next设置为NULL,初始长度为0的单链表}void createlist(Linklist L) //创建...

天峻县19825675577: 数据结构(C语言) 单链表的合并(100分) -
子丰萱鼻渊: #include<stdio.h>#include<time.h>#include<stdlib.h> typedef struct list { long value; struct list * next; }list; void CreatList(list * p,int length) {int i,value; list *r,*s; for(i=0;i<length;i++) { value=rand();r=p; while(1) { if(r->next==NULL) { r->next=(list*)malloc(...

天峻县19825675577: 求任意两个正数集合的并、交和差集.用C语言做. -
子丰萱鼻渊: 数字有什么特征: 1. 都是正整数么? 2. 数字的范围是多少? 3. 每个结合内部有没有重复的数字?如果都是正整数,并且每个集合都没有重复数字,那问题就相当好办了,用直接存储的数据结构即可.“交:从一个集合中取出一个元素,在另一个集合中查找,如果有它就是交中的并:并也类似,关键就是判断这个元素是否都在这两个集合中出现.差:差更 ”这种方式效率太低,当结合非常大的时候,时间复杂度回事O(N^2),用直接存储可以保证时间复杂度为O(N).

天峻县19825675577: C语言实现集合合并的问题 -
子丰萱鼻渊: C语言没有提供这样的功能,只能自己写个函数了 但是我感觉你所谓的“合并”很模糊. 如果是12和34,合并是变成了46呢还是1234? 如果是char数组,难道合并以后这个变成了两个字符码? 把这些考虑清楚了就可以自己写个函数,如果不会...

天峻县19825675577: 数据结构 c语言编写顺序表合并 -
子丰萱鼻渊: /***太多错误了,包括语法和逻辑上的错误都有....**我修改了一下,现在可以了.**请注意,下面我说的字符串均指纯数字字符串,这个程序中是以字符方式来处理成数字的**输入的时候,第一次输入的必须是顺序串(否则还要加一个排序...

天峻县19825675577: 数据结构,用C语言编程.写出C=AUB就是顺序表的合并.急求大神!!!! -
子丰萱鼻渊: typedef struct { ElemType* elem; int length; int listsize; }SqList; void Union(SqList La,SqList Lb,SqList& Lc){ for(int i=k=0;i Lc.elem[k++] = La.elem[i]; }for(int j=0;j for(int i = 0;i if(i == La.length) Lc.elem[k++] = Lb.elem[j]; } Lc.length = k;//修改表长 }

天峻县19825675577: 本人碰见一道C语言难题,寻大神帮助,利用C语言实现:求任意两个集合的交集、并集、差集, -
子丰萱鼻渊: 具体忘了,利用链表可以求解,参考严蔚敏 数据结构(C语言版)

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网