一.算法思想
实现思路与在顺序表中合并两个递增的有序表类似,递减可由头插法来实现
二.源代码
void MergeList(List La,lList Lb){
Node pa,pb,r;
pa = La->next;
pb = Lb->next;
La->next = Null;//将La作为结果链表的头指针
while(pa&&pb){
if(pa->data < pb->data){
r = pa->next;//r暂存pa的后继结点指针
/*头插法插入结点*/
pa->next = La->next;
La->next = pa;
pa = r; //将p恢复为工作指针
}else{
r = pb->next;//r暂存pb的后继结点指针
/*头插法插入结点*/
pb->next = Lb->next;
La->next = pb;
pb = r; //将b恢复为工作指针
}
}
while(pa){ //pb为空
r = pa->next;//r暂存pa的后继结点指针
/*头插法插入结点*/
pa->next = La->next;
La->next = pa;
pa = r; //将p恢复为工作指针
}
while(pb){ //pa为空
r = pb->next;//r暂存pb的后继结点指针
/*头插法插入结点*/
pb->next = La->next;
La->next = pb;
pb = r; //将p恢复为工作指针
}
}