版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21149391/article/details/79517629
一、思路:
构造struct node* Link(struct node *P,struct node *Q,int sign)函数,当sign=1时,返回P,Q的并集,当sign=0时,返回P,Q的交集,求交并的思路为:
①对P,Q分别赋予两个指针p和q,初始时分别指向P,Q的头结点
建立两个空链表U(Union)和I(Intersection),用于存P,Q的并集与交集,同样赋予两个初始指向它们头的指针u和i
②判断p,q的值,将值小的插入并集U链表的后面,即u的后面,并且u指向下一位,同时这个节点也向后移一位;
若p,q值相同,则将此节点插入交集I和并集U链表的后面,即i和u的后面,并且i和u指向下一位,同时p,q都向后移一位;
并且,对于单调不减的函数,存在有相同值的节点的情况,故每次移位前都判断一下,下一位是否跟此位的值相同,若是,则再向后移一位。
一直循环上述,直至p,或q为空
③可能存在三种情况:
1)p空,q空,则全部遍历并插入完毕
2)p空,q非空,则将q后剩余节点全部插入并集U
3)p非空,q空,同理,则将p后剩余节点全部插入并集U
④根据sign值返回U或I
二、代码:
(我将单链表的结构与构造P,Q均放在了Node.h中)
单链表的结构为:
struct node{
int value;
struct node *next;
};
构造P,Q():
/*创建链表P*/
struct node *creatL1(void){
struct node *p1,*p2,*p3,*p4,*p5,*p6,*p7;
p1=(struct node *)malloc(LEN);
p2=(struct node *)malloc(LEN);
p3=(struct node *)malloc(LEN);
p4=(struct node *)malloc(LEN);
p5=(struct node *)malloc(LEN);
p6=(struct node *)malloc(LEN);
p7=(struct node *)malloc(LEN);
p1->value = 1;
p2->value = 3;
p3->value = 6;
p4->value = 6;
p5->value = 6;
p6->value = 13;
p7->value = 15;
p1->next = p2;
p2->next = p3;
p3->next = p4;
p4->next = p5;
p5->next = p6;
p6->next = p7;
p7->next = NULL;
return(p1);
}
/*创建链表Q*/
struct node *creatL2(void){
struct node *p1,*p2,*p3,*p4,*p5,*p6;
p1=(struct node *)malloc(LEN);
p2=(struct node *)malloc(LEN);
p3=(struct node *)malloc(LEN);
p4=(struct node *)malloc(LEN);
p5=(struct node *)malloc(LEN);
p6=(struct node *)malloc(LEN);
p1->value = 2;
p2->value = 3;
p3->value = 6;
p4->value = 8;
p5->value = 15;
p6->value = 17;
p1->next = p2;
p2->next = p3;
p3->next = p4;
p4->next = p5;
p5->next = p6;
p6->next = NULL;
return(p1);
}
主函数:
#include<stdio.h>
#include"Node.h"
#define LEN sizeof(struct node)
int main(){
struct node* Link(struct node *P,struct node *Q,int sign);
void Print(struct node *P);
struct node *I,*U;
//输出一下P,Q情况
printf("P为:\n");
Print(creatL1());
printf("Q为:\n");
Print(creatL2());
U=Link(creatL1(),creatL2(),1);
I=Link(creatL1(),creatL2(),0);
//输出交集并集
printf("P,Q的并集为:\n");
Print(U->next);
printf("P,Q的交集为:\n");
Print(I->next);
}
struct node* Link(struct node *P,struct node *Q,int sign){
struct node *u,*U,*i,*I;
struct node *p=P,*q=Q;
u = U=(struct node *)malloc(LEN);
i = I=(struct node *)malloc(LEN);
//②步骤,循环插入
while(p!=NULL && q!=NULL){
if(p->value > q->value){
u->next = q;
u = u->next;
while(q->next!=NULL && q->next->value == q->value)//过滤掉相等的节点
q = q->next;
q = q->next;
}else if(p->value < q->value){
u->next = p;
u = u->next;
while(p->next!=NULL && p->next->value == p->value)
p = p->next;
p = p->next;
}
else if(p->value == q->value){
i->next = q;
u->next = p;
i = i->next;
u = u->next;
while(q->next!=NULL && q->next->value == q->value)
q = q->next;
while(p->next!=NULL && p->next->value == p->value)
p = p->next;
q = q->next;
p = p->next;
}
}
i->next = NULL;
//③步骤 ,处理剩余
if(q!=NULL)
u->next = q;
else if(p!=NULL)
u->next = p;
if(sign ==1)
return U;
else if(sign == 0)
return I;
}
//打印输出一个链表的全部数据
void Print(struct node *P){
struct node *p = P;
while(p->next!=NULL){
printf("%d->",p->value);
p = p->next;
}
printf("%d\n",p->value);
}
三、运行结构: