实在不知道哪里有问题(两个顺序表La,Lb合并成Lc,但是Lc输出乱码)
程序代码:
#include<stdio.h> #include<malloc.h> #define OK 1 #define ERROR 0 #define LIST_INIT_SIZE 100 #define LISTINCREMMENT 10 #define ElemType int typedef struct {int *elem; int listlength; int listsize;}sqlist; int initlist(sqlist *L) {L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); L->listlength=0; L->listsize=LIST_INIT_SIZE; return OK;} int loadsq(sqlist *L) {int i; for(i=0;i<L->listlength;i++) printf(" %d",L->elem[i]); printf("\n"); return OK;} int listlength(sqlist *L) {return L->listlength;} int getelem(sqlist *L,int i,int e) {e=L->elem[i-1]; return OK;} int listinsert(sqlist *L,int i,int e) {ElemType *newbase,*p,*q; if(i<1||i>L->listlength+1) return ERROR; if(L->listlength>=L->listsize) {newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMMENT)*sizeof(ElemType)); if(!newbase) return ERROR; L->elem=newbase; L->listsize+=LISTINCREMMENT;} q=&(L->elem[i-1]); for(p=&(L->elem[L->listlength-1]);p>=q;--p) *(p+1)=*p; *q=e; ++(L->listlength); return OK; } void mergelist(sqlist *La,sqlist *Lb,sqlist *Lc) { int i,j,k,La_len,Lb_len,ai,bj; i=j=1;k=0; La_len=listlength(La);Lb_len=listlength(Lb); while((i<=La_len)&&(j<=Lb_len)) {getelem(La,i,ai);getelem(Lb,j,bj); if(ai<=bj) {listinsert(Lc,++k,ai);++i;loadsq(Lc);} else {listinsert(Lc,++k,bj);++j;}} while(i<=La_len) {getelem(La,i++,ai);listinsert(Lc,++k,ai);} while(j<=Lb_len) {getelem(Lb,j++,bj);listinsert(Lc,++k,bj);} loadsq(Lc);} int main() {int an,bn,i,e; sqlist La,Lb,Lc; initlist(&La); scanf("%d",&an); for(i=1;i<=an;i++) {scanf("%d",&e); listinsert(&La,i,e);} printf("LIST A is:"); loadsq(&La); initlist(&Lb); scanf("%d",&bn); for(i=1;i<=bn;i++) {scanf("%d",&e); listinsert(&Lb,i,e);} printf("LIST B is:"); loadsq(&Lb); printf("LIST C is :"); initlist(&Lc); mergelist(&La,&Lb,&Lc); return 0; }