rainymay 发表于 2013-12-8 15:46:00

数据结构编程问题求解

#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
struct list {
   elemtype *list;
   int size;
   int maxsize;
};
void againmalloc(struct list *L)
{
    elemtype *p=realloc(L->list,(2*L->maxsize)*sizeof(elemtype));
    if(!p){
      printf("存储空间分配失败!");
      exit(1);
    }
    L->list=p;
    L->maxsize=2*L->maxsize;
}
void insertlastlist(struct list *L,elemtype x)
{
   if(L->size=L->maxsize){
      againmalloc(&L);
   }
   L->list=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->list=(elemtype*)malloc(ms*sizeof(elemtype));
   if(!L->list){
    printf("空间分配失败!");
    exit(1);
   }
   return ;
}
void insertposlist(struct list *L,int pos,elemtype x)
{
   int i;
   if(pos<1||pos>L->size+1){
      return 0;
   }
   if(L->size==L->maxsize){
      againmalloc(&L);
   }
   for(i=L->size-1;i>=pos-1;i--){
      L->list=L->list;

   }
   L->list=x;
   L->size++;
   return ;
}
void MergeList(struct list *La, struct list *Lb)
{
    int j=0,i=0,m=0;
    La->list=(elemtype *)realloc(La->list,(La->maxsize+Lb->maxsize)*sizeof(elemtype));
    La->maxsize=La->maxsize+Lb->maxsize;
    for(i;i<La->size+Lb->size;i++){
      if(La->list<Lb->list){
            i++;
      }
      else if(La->list>=Lb->list){
            insertposlist(La, i,Lb->list);
            j++;
      }
    }
    for(m;m<=i;m++){
      printf("%d ",&La->list);
   }
    return ;
}
void main()
{
    int i,k;
    int a={0,2,4,6,8};
    int b={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);
      insertlastlist(&Lb,b);
    }
    MergeList(&La,&Lb);
    getchar();
    return 0;

}
显示内存分配失败,不知错在哪里,求大神解答!


页: [1]
查看完整版本: 数据结构编程问题求解