chenxi16

导航

数据结构-1 带头结点的链表合并

 

 

 

 

List Merge( List L1, List L2 ){
struct Node *h,*p,*l1,*l2;
h = (struct Node*)malloc(sizeof(struct Node));
h->Next = NULL;
p = h;
l1 = L1->Next;
l2 = L2->Next;
while(l1!=NULL && l2!=NULL){
if(l1->Data > l2->Data){
p->Next = l2;
p = l2;
l2 = l2->Next;
}
else if(l1->Data <= l2->Data){
p->Next = l1;
p = l1;
l1 = l1->Next;
}
//else{
// p->Next = l1;
// p = l1;
// l1 = l1->Next;
// l2 = l2->Next;
//}
}
while(l1==NULL && l2!=NULL){
p->Next = l2;
p = l2;
l2 = l2->Next;
}
while(l1!=NULL && l2==NULL){
p->Next = l1;
p = l1;
l1 = l1->Next;
}
p->Next = NULL;
L1->Next = NULL;
L2->Next = NULL;

return h;
}
注意:
1. 链表合并以后,相同的元素应该都放在链表中,而不应该保存为一个值。
2.表头结点必须新申请一个空间。

 

posted on 2020-03-30 18:31  chenxi16  阅读(182)  评论(0编辑  收藏  举报