求助!!数据结构顺序表(把两张表升序插入一张空表)MergeList函数哪错了
#include "stdio.h"#include "malloc.h"
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
ListInit(SqList &L){ /*初始化顺序表*/
L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
L.length=0;
L.listsize=LIST_INIT_SIZE;
return 1;
}
ListInsert(SqList &L,int i,ElemType e){ /*在顺序表里插入元素*/
ElemType *p,*q;
if(i<1 || i>L.length+1) {printf("输入的位置不合法!\n");return 0;}
if(L.length==L.listsize) {
L.elem =(ElemType *)realloc(L.elem,sizeof(ElemType)*10+L.listsize);
L.listsize+=sizeof(ElemType)*10;
}
q=&L.elem[i-1];
for(p=&(L.elem[L.length-1]);p>=q;p--) *(p+1)=*p;
*q=e;
L.length++;
return 1;
}
Status ListDelete(SqList &L,int i){ /*删除顺序表中的一个元素*/
if(i<1||i>L.length+1) {printf("删除的位置不合法!\n");return 0;}
ElemType *q,*p;
q=&(L.elem[i-1]);
for(p=(L.elem+L.length-1);q<=p;p--) *(p-1)=*p;L.length--;
printf("删除成功!\n");
printf("现在表长度是:%d\n\n",L.length);
return 1;
}
Status ListLength(SqList &L){ /*获取顺序表的长度*/
return L.length;
}
Status GetElem(SqList &L,int i,ElemType &e){ /*获取顺序表中的某个元素*/
if(i<1||i>L.length) {
return 0;
}
e=L.elem[i-1];
return 1;
}
Status LocateElem(SqList &L,ElemType e){ /*判断顺序表中是否有某个元素*/
int i;
for(i=0;i<L.length;i++)
if(L.elem[i]==e)
return 1;
else
return 0;
}
void ListPrint(SqList &L){ /*打印顺序表所以元素*/
int i;
printf("此表有元素:\n");
for(i=0;i<L.length;i++)
printf("\t %d \n",L.elem[i]);
}
void unio(SqList &L1,SqList L2,ElemType &e){ /*合并两张顺序表*/
int j;
for(j=1;j<=ListLength(L2);j++){
GetElem(L2,j,e);
if(!LocateElem(L1,e))
ListInsert(L1,L1.length,e);
}
}
void MergeList(SqList La,SqList Lb,SqList &Lc){ /*合并两张表,并且升序排列*/
int i,j;
int k=1;
int La_len,Lb_len;
ElemType ai,aj;
ListInit(Lc);
i=j=1;
La_len=ListLength(La);
Lb_len=ListLength(Lb);
while(i<=La_len&&j<=Lb_len){
GetElem(La,i,ai);
GetElem(Lb,j,aj);
if(ai<=aj){
ListInsert(Lc,k++,ai);
i++;
}
else{
ListInsert(Lc,k++,aj);
j++;
}
while(i<=La_len){
GetElem(La,i,ai);
ListInsert(Lc,k,ai);
}
while(j<=Lb_len){
GetElem(Lb,j,aj);
ListInsert(Lc,k,aj);
}
}
}
void main(){SqList a1,a2,aa;
/* ElemType EE; */
ListInit(a1);
ListInit(a2);
ListInsert(a1,1,1);
ListInsert(a1,1,5);
ListInsert(a1,1,3);
ListInsert(a2,1,4);
ListInsert(a2,1,2);
ListInsert(a2,1,6);
printf("a1表:\n");
ListPrint(a1);
printf("a2表:\n");
ListPrint(a2);
/*printf("两表合并为a1:\n");
unio(a1,a2,EE);
ListPrint(a1);*/
printf("两表合并为a3(升序排列):\n");
MergeList(a1,a2,aa);
ListPrint(aa);
}