赞
踩
Lnode mergelist(LinkList &a,LinkList &b){ //将两个有序链表合并为一个链表
LinkList *Lc = new Lnode;
Lnode *pa,*pb,*pc;
pa = La->next; //工作指针pa
pb = Lb->next;
pc = Lc = La; //合并后的新链Lc
while (pa&&pb){ //当pa或者pb其中有一个链为空时停止循环
if(pa->data<pb->data){ //pa.data小于pb.data时
pc->next = pa; //保证链表的连续
pc=pa; //pc向后走
pa=pa->next; //pa向后走
}
else if(pa->data>pb->data){ //pa.data大于pb.data时
pc->next = pb;
pc=pb;
pb=pb->next;
}
else { //pa.data等于pb.data时
pc->next=pa;
pc=pa;
pa=pa->next;
q=pb->next;
delete pb;
pb = q;
}
- 注意:delete 删除pb时,删除的是pb指向的内容而不是pb这个指针变量
pc->next = pa?pa:pb;
delete Lb;
Lnode *mergelist(LinkList &a,LinkList &b){ //将两个有序链表合并为一个链表 LinkList *Lc = new Lnode; Lnode *pa,*pb,*pc; pa = La->next; //工作指针pa pb = Lb->next; pc = Lc = La; //合并后的新链Lc while (pa&&pb){ //当pa或者pb其中有一个链为空时停止循环 if(pa->data<pb->data){ //pa.data小于pb.data时 pc->next = pa; //保证链表的连续 pc=pa; //pc向后走 pa=pa->next; //pa向后走 } else if(pa->data>pb->data){ //pa.data大于pb.data时 pc->next = pb; pc=pb; pb=pb->next; } else { //pa.data等于pb.data时 pc->next=pa; pc=pa; pa=pa->next; q=pb->next; delete pb; pb = q; } } pc->next = pa?pa:pb; delete Lb; return Lc; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。