两个非递减有序顺序表A和B合并成一个新的非递减有序顺序表C,求修改啊
程序代码:
#include<stdio.h> #include<malloc.h> #define OK 1 #define ERROR 0 #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define ElemType int typedef struct { int *elem; int length; int listsize; }List; int InitList(List *L,int n) { L->elem=(ElemType *)malloc(n*sizeof(ElemType)); if(!L->elem) return(ERROR); L->length=n; L->listsize=n; return OK; } void MergeList(List *La,List *Lb,List *Lc) { int *pa = La->elem; int *pb = Lb->elem; int *pa_last,*pb_last,*pc; Lc->listsize = La->length + Lb->length; Lc->length = Lc->listsize; pc = Lc->elem = (ElemType * )malloc(Lc->listsize * sizeof(ElemType)); pa_last = La->elem + La->length; pb_last = Lb->elem + Lb->length; while((pa<pa_last) &&(pb<pb_last)) { if(*pa<=*pb) *pc++ = *pa++; else *pc++ = *pb++; } while(pa<pa_last) *pc++ = *pa++; while(pb<pb_last) *pc++ = *pb++; } int Load(List *L) { int i; if(!L->length) printf("The List is empty!"); else { printf("The List is: "); for(i=0;i<=L->length-1;i++) printf("%d ",L->elem[i]); } printf("\n"); return OK; } int main() { int i=0,j=0,n; List La,Lb,Lc; printf("请输入一个数字\n"); scanf("%d",&n); InitList(&La,n); for(;i<n;i++) { scanf("%d",&(La.elem[i])); } printf("请输入一个数字\n"); scanf("%d",&n); InitList(&Lb,n); for(;j<n;j++) { scanf("%d",&(Lb.elem[j])); } InitList(&Lc,i+j); MergeList(&La,&Lb,&Lc); Load(&La); Load(&Lb); Load(&Lc); return 0; }
最后输出Lc的结果是一堆地址,修改了好久啊,求救。