请设计一个算法,,求A和B两个单链表表示的集合的交集、并集、差集
答案:3 悬赏:10
解决时间 2021-04-28 15:17
- 提问者网友:清羽墨安
- 2021-04-27 18:04
设计一个算法,,求A和B两个单链表表示的集合的交集、并集、差集
最佳答案
- 二级知识专家网友:一池湖水
- 2021-04-27 18:23
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{char data;
struct Node*next;
}*linklist;
void readdata(linklist head)
{ linklist p;
char a;
scanf("%c",&a);
while(a!='\n')
{ p=(linklist)malloc(sizeof(structNode));
p->data=a;
p->next=head->next;
head->next=p;
scanf("%c",&a);
}
}
void pop(linklist head)
{ linklist p;
p=head->next;
while(p!=NULL)
{ printf("%c",p->data);
p=p->next;
}
printf("\n");
}
void bingji(linklist head1,linklisthead2,linklist head3)
{ linklist 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=(linklist)malloc(sizeof(struct Node));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3; }
p1=p1->next; } }
void jiaoji(linklist head1,linklisthead2,linklist head3)
{ linklist p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{ p3=(linklist)malloc(sizeof(struct Node));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
p1=p1->next; }
p2=head2->next;
while(p2!=NULL)
{ p1=head1->next;
while((p1!=NULL)&&(p1->data!=p2->data))
p1=p1->next;
if (p1==NULL)
{ p3=(linklist)malloc(sizeof(struct Node));
p3->data=p2->data;
p3->next=head3->next;
head3->next=p3; }
p2=p2->next;
}
}
void chaji(linklist head1,linklisthead2,linklist head3)
{ linklist 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)
{ p3=(linklist)malloc(sizeof(struct Node));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void main(linklist head1,linklisthead2,linklist head3)
{
int x;
printf("input values and end up with ‘enter’ \n");
head1=(linklist)malloc(sizeof(struct Node));
head1->next=NULL;
head2=(linklist)malloc(sizeof(struct Node));
head2->next=NULL;
head3=(linklist)malloc(sizeof(struct Node));
head3->next=NULL;
printf("please input ji he 1 \n");
readdata(head1);
printf("please input ji he 2 \n"); //调用链表输出函数链表数据
readdata(head2);
A:printf("1.bing ji 2.jiaoji 3.cha ji 4.exit \n");
do{
printf("qing xuan ze xu hao\n");
scanf("%d",&x);
switch(x)
{
case 1:
printf("liang ge ji he debing ji shi \n");
jiaoji(head1,head2,head3);
pop(head3);
head3->next=NULL;
break;
case 2:
printf("liang ge ji he dejiao ji shi \n");
bingji(head1,head2,head3);
pop(head3);
head3->next=NULL;
break;
case 3:
printf("liang ge ji he decha ji shi \n");
chaji(head1,head2,head3);
pop(head3);
head3->next=NULL;
break;
case 4: break;
default:goto A; }
}
while(x!=4);
}
#include<stdlib.h>
typedef struct Node
{char data;
struct Node*next;
}*linklist;
void readdata(linklist head)
{ linklist p;
char a;
scanf("%c",&a);
while(a!='\n')
{ p=(linklist)malloc(sizeof(structNode));
p->data=a;
p->next=head->next;
head->next=p;
scanf("%c",&a);
}
}
void pop(linklist head)
{ linklist p;
p=head->next;
while(p!=NULL)
{ printf("%c",p->data);
p=p->next;
}
printf("\n");
}
void bingji(linklist head1,linklisthead2,linklist head3)
{ linklist 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=(linklist)malloc(sizeof(struct Node));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3; }
p1=p1->next; } }
void jiaoji(linklist head1,linklisthead2,linklist head3)
{ linklist p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{ p3=(linklist)malloc(sizeof(struct Node));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
p1=p1->next; }
p2=head2->next;
while(p2!=NULL)
{ p1=head1->next;
while((p1!=NULL)&&(p1->data!=p2->data))
p1=p1->next;
if (p1==NULL)
{ p3=(linklist)malloc(sizeof(struct Node));
p3->data=p2->data;
p3->next=head3->next;
head3->next=p3; }
p2=p2->next;
}
}
void chaji(linklist head1,linklisthead2,linklist head3)
{ linklist 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)
{ p3=(linklist)malloc(sizeof(struct Node));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void main(linklist head1,linklisthead2,linklist head3)
{
int x;
printf("input values and end up with ‘enter’ \n");
head1=(linklist)malloc(sizeof(struct Node));
head1->next=NULL;
head2=(linklist)malloc(sizeof(struct Node));
head2->next=NULL;
head3=(linklist)malloc(sizeof(struct Node));
head3->next=NULL;
printf("please input ji he 1 \n");
readdata(head1);
printf("please input ji he 2 \n"); //调用链表输出函数链表数据
readdata(head2);
A:printf("1.bing ji 2.jiaoji 3.cha ji 4.exit \n");
do{
printf("qing xuan ze xu hao\n");
scanf("%d",&x);
switch(x)
{
case 1:
printf("liang ge ji he debing ji shi \n");
jiaoji(head1,head2,head3);
pop(head3);
head3->next=NULL;
break;
case 2:
printf("liang ge ji he dejiao ji shi \n");
bingji(head1,head2,head3);
pop(head3);
head3->next=NULL;
break;
case 3:
printf("liang ge ji he decha ji shi \n");
chaji(head1,head2,head3);
pop(head3);
head3->next=NULL;
break;
case 4: break;
default:goto A; }
}
while(x!=4);
}
全部回答
- 1楼网友:為→妳鎖鈊
- 2021-04-27 19:29
递增无序?递增有序吧,如果无序,首先给两个链表排序,以下代码按升序合并
先剔除两条链表里的相同值,然后再合并
linklist elimtlist(linklist l)
{
linklist p = null;
linklist t = null;
linklist tmp = null;
p = l->next;
t = p->next;
while (null != t)
{
if (p->num == t->num)
{
tmp = t->next;
p->next = tmp;
free(t);
t = tmp;
}
else
{
p = p->next;
t = t->next;
}
}
return(l);
}
linklist mergelist(linklist l1, linklist l2)
{
linklist p = null;
linklist p1 = null;
linklist p2 = null;
linklist tmp = null;
p = l1;
p1 = l1->next;
p2 = l2->next;
while ((null != p1) && (null != p2))
{
if (p1->num < p2->num)
{
p = p1;
p1 = p1->next;
}
else if (p1->num > p2->num)
{
tmp = p2->next;
p2->next = p1;
p->next = p2;
p = p2;
p2 = tmp;
}
else
{
tmp = p2->next;
free(p2);
p2 = tmp;
}
}
p->next = p1 ? p1 : p2;
free(l2);
return(l1);
}
如果对你有所帮助,请记得采纳最佳答案,谢谢!
- 2楼网友:万千宠爱
- 2021-04-27 19:03
你好哦。
很高兴看到你的问题。
但是又很遗憾到现在还没有人回答你的问题。也可能你现在已经在别的地方找到了答案,那就得恭喜你啦。
可能是你问的问题有些专业了,没人会。或者别人没有遇到或者接触过你的问题,所以帮不了你。建议你去问题的相关论坛去求助,那里的人通常比较多,也比较热心,可能能快点帮你解决问题。
祝你好运~!
希望我的回答也能够帮到你!
谢谢
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息