合并顺序表A和B,并且升序排序 到&Lc

void union(List &La,List Lb){ //线性表 求并集
    //求并集
    //不存在的元素插入到La
    La_len = ListLength(La);
    Lb_len = ListLength(Lb);
    for(i=1;I<=Lb_len;i++){
        GetElem(Lb,i,e);
        if(!LocateElem(La,e,equal)){
            ListInsert(La,++La_len,e);
        }
    }//for
}//union

void MergeList(List La,List Lb,List &Lc){
    //合并顺序表,并且升序排序 到&Lc
    pa = La.elem;pb = Lb.elem;//取头部指针a,b
    Lc.listsize = Lc.length = La.length +Lb.length;
    pc = Lc.elem = (ElemType*)malloc(Lc.listsize*sizeof(ElemType));//取指针,分配内存
    if(!Lc.elem) exit(OVERFLOW);//分配内存失败
    pa.last = La.elem +La.length -1;//取尾部指针a
    pb.last = Lb.elem +Lb.length -1;//取尾部指针b
    while(pa<=pa.last && pb<=pb.last){ //开始合并
        if(*pa <= *pb){
            * pc++ = * pa++;//a存入c
        }
        else{
            * pc++ = * pb++;//b存入c
        }        
    }
    while(pa<=pa.last){ * pc++ = * pa++;}//剩余元素a存入c 
    while(pb<=pb.last){ * pc++ = * pb++;}//剩余元素b存入c
}//MergeList

 

posted @ 2017-02-22 12:28  影落明湖  阅读(699)  评论(0编辑  收藏  举报