//破坏原有的链表实现合并: void MergeList(List La,List Lb,List &Lc) { ListClear(Lc); ElemType ea,eb; while(!ListEmpty(La)&&!ListEmpty(Lb)){ GetElem(La,1,ea); GetElem(Lb,1,eb); if(ea<eb){ ListInsert(Lc,ListLength(Lc)+1,ea); DeleteList(La,2); } else{ ListInsert(Lc,ListLength(Lc)+1,eb); DeleteList(Lb,2); } } if(ListEmpty(La)){ while(!ListEmpty(Lb)){ GetElem(Lb,1,eb); ListInsert(Lc,ListLength(Lc)+1,eb); DeleteList(Lb,2); } } else{
while(!ListEmpty(La)){
GetElem(Lb,1,ea);
ListInsert(Lc,ListLength(Lc)+1,ea);
DeleteList(La,2);
}
}
}
//非破坏性的合并:(使用标记来定位线性表元素的位置)
void MergeList(List La,List Lb,List &Lc){
int indexa=indexb=1;
int k=0; ElemType ea,eb;
int len_La=ListLength(La);
int len_Lb=ListLength(Lb);
InitList(Lc);
while((indexa<=len_La)&&(indexb<=len_Lb)){//线性表逻辑非空
GetElem(La,indexa);
GetElem(Lb,indexb);
if(ea<=eb){
ListInsert(Lc,++k,ea);
++indexa;
}
else{
ListInsert(Lc,++k,eb);
++indexb;
}
}
//剩下单个线性表:使用循环顺序插入
while(indexb<=len_Lb){//线性表b逻辑非空
GetElem(Lb,indexb,eb);
ListInsert(Lc,++k,eb);
}
while(indexa<=len_La){//线性表a逻辑非空
GetElem(La,indexa,ea);
ListInsert(Lc,++k,ea);
}
}
非递减顺序表的合并
最新推荐文章于 2023-03-30 21:59:47 发布