单链表相关算法
求交集并集
void Union(NODE l1,NODE l2){
NODE p1,p2,p3;
p1 = l1->next;
p2 = l2->next;
p3 = l1;
while(p1 != NULL && p2 != NULL){
if(p1->value <= p2->value){
p3->next = p1;
p3 = p1;
p1 = p1->next;
} else {
p3->next = p2;
p3 = p2;
p2 = p2->next;
}
}
p3->next = p2 ? p2 : p1;
}
void Intersection(NODE l1,NODE l2){
NODE p1,p2,p3;
NODE q;
p1 = l1->next;
p2 = l2->next;
p3 = l1;
p3->next = NULL;
while(p1 != NULL && p2 != NULL){
if(p1->value < p2->value){
p1 = p1->next;
} else if (p1->value > p2->value) {
p2 = p2->next;
} else {
q = p2->next;
p2->next = p3->next;
p3->next = p2;
p3 = p2;
p2 = q;
}
}
}
|