第二章线性表(续)一、用单链表存储结构实现两个线性表的归并二、一元多项式的表示及相加(简介)一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpa=La-next;pb=Lb-next;Lc=pc=La;pc一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpa=La-next;pb=Lb-next;Lc=pc=La;pc一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}^一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}if(pb)pc-next=pb;elsepc-next=pa;^一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:La35811^Lb26891115^20Lcpapbpcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}if(pb)pc-next=pb;elsepc-next=pa;^free(Lb);一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:35811^26891115^20Lcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}if(pb)pc-next=pb;elsepc-next=pa;一、两个线性表的归并引例:La=(3,5,8,11)Lb=(2,6,8,9,11,15,20)则:求La与Lb的归并,得结果线性表Lc如下:Lc=(2,3,5,6,8,8,9,11,11,15,20)单链表的物理实现过程:35811^26891115^20Lcwhile(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=pb;pc=pb;pb=pb-next;}}if(pb)pc-next=pb;elsepc-next=pa;一、两个线性表的归并归并算法:voidMergeList_L(LinkList&La,LinkList&Lb,LinkList&Lc){pa=La-next;pb=Lb-next;Lc=pc=La;while(pa&&pb){if(pa-data=pb-data){pc-next=pa;pc=pa;pa=pa-next;}else{pc-next=