|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//将线性表Lb中元素插入到La中,使得La中元素自小到大
请问我下面写的代码错在哪了?????
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
struct list {
elemtype *elem;
int size;
int maxsize;
};
void againmalloc(struct list *L)
{
elemtype *p=realloc(L->elem,(2*L->maxsize)*sizeof(elemtype));
if(!p){
printf("存储空间分配失败!");
exit(1);
}
L->elem=p;
L->maxsize=2*L->maxsize;
}
//将2个线性表中的整型指针内赋初值
void insertlastlist(struct list *L,elemtype x)
{
if(L->size==L->maxsize){
againmalloc(L);
}
L->elem[L->size]=x;
L->size++;
return;
}
//开辟空间并赋值
void initlist(struct list *L,int ms)
{
if (ms<=0){
printf("maxsize非法!");
exit(1);
}
L->maxsize=ms;
L->size=0;
L->elem=(elemtype*)malloc(ms*sizeof(elemtype));
if(!L->elem){
printf("空间分配失败!");
exit(1);
}
return ;
}
void insertposlist(struct list *L,int pos,elemtype x)
{
int i;
if(pos<1||pos>L->size+1){
printf("插入点错误");
return ;
}
if(L->size==L->maxsize){
againmalloc(L);
}
for(i=L->size-1;i>=pos-1;i--){
L->elem[i+1]=L->elem[i];
}
L->elem[i]=x;
L->size++;
return ;
}
//将线性表Lb中元素插入到La中,使得La中元素从小到大
void MergeList(struct list *La, struct list *Lb)
{
int j=0,i=0,m=0,n;
La->elem=(elemtype *)realloc(La->elem,(La->maxsize+Lb->maxsize)*sizeof(elemtype));
La->maxsize=La->maxsize+Lb->maxsize;
n=La->size;
La->size=La->size+Lb->size;
for(i;i<La->size;i++){
if(i==n+j){
for(j;j<Lb->size;j++){
La->elem[i++]=Lb->elem[j];
}
break;
}
if(La->elem>=Lb->elem[j]){
insertposlist(La,i+1,Lb->elem[j]);
j++;
}
}
// 打印La中的元素
for(m;m<La->size;m++){
printf("%d ",La->elem[m]);
}
return ;
}
void main()
{
int i,k;
int a[5]={0,2,4,6,8};
int b[5]={1,3,5,7,9};
struct list La;
struct list Lb;
initlist(&La,5);
initlist(&Lb,5);
for(k=0;k<5;k++){
insertlastlist(&La,a[k]);
insertlastlist(&Lb,b[k]);
}
MergeList(&La,&Lb);
getchar();
}
[/i][/i] |
|