鱼C论坛

 找回密码
 立即注册
查看: 2771|回复: 0

数据结构编程问题求解

[复制链接]
发表于 2013-12-8 15:46:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#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[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->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[i+1]=L->list[i];

     }
     L->list[i]=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[i]<Lb->list[j]){
            i++;
        }
        else if(La->list[i]>=Lb->list[j]){
            insertposlist(La, i,Lb->list[j]);
            j++;
        }
    }
    for(m;m<=i;m++){
        printf("%d ",&La->list[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();
    return 0;

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


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 22:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表