C语言数据结构程序调试出错,找不到问题在哪,求大神们解决。?
#include<stdio.h>#include<stdlib.h>#defineMAXSIZE30000#defineOVERFLOW-2#defineERROR0#...
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 30000
#define OVERFLOW -2
#define ERROR 0
#define OK 1
typedef int Status;
typedef struct
{
int *elem;
int length;
}SqList;
Status GetElem(SqList *L,int m,int *e)
{
int i;
L->elem=malloc(m);
for(i=1;i<=m;i++)
{
scanf("%d",&e);
L->elem[i-1]=*e;
e++;
}
}
void MergeList(SqList LA,SqList LB,SqList *LC)
{
int *pa,*pb,*pc;
int *pa_last,*pb_last;
LC->length=LA.length+LB.length;
LC->elem=malloc(LC->length);
pa=LA.elem;
pb=LB.elem;
pc=LC->elem;
pa_last=LA.elem[LA.length-1];
pb_last=LB.elem[LB.length-1];
while((pa<=pa_last)&&(pb<=pb_last))
{
if(*pa<=*pb)
{
*pc=*pa;
pc++;
pa++;
}
else
{
*pc=*pb;
pb++;
pc++;
}
}
while(pa<=pa_last)
{
*pc=*pa;
pc++;
pa++;
}
while(pb<=pb_last)
{
*pc=*pb;
pc++;
pb++;
}
}
int main()
{
int n,a,b,i,j,*e;
SqList *LA,*LB,*LC;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Case #%d:\n",i);
scanf("%d",&a);
GetElem(LA,a,e);
scanf("%d",&b);
GetElem(LB,b,e);
}
}
调试了几次感觉是内存分配的错误,到底是哪里错了应该怎么改啊。 展开
#include<stdlib.h>
#define MAXSIZE 30000
#define OVERFLOW -2
#define ERROR 0
#define OK 1
typedef int Status;
typedef struct
{
int *elem;
int length;
}SqList;
Status GetElem(SqList *L,int m,int *e)
{
int i;
L->elem=malloc(m);
for(i=1;i<=m;i++)
{
scanf("%d",&e);
L->elem[i-1]=*e;
e++;
}
}
void MergeList(SqList LA,SqList LB,SqList *LC)
{
int *pa,*pb,*pc;
int *pa_last,*pb_last;
LC->length=LA.length+LB.length;
LC->elem=malloc(LC->length);
pa=LA.elem;
pb=LB.elem;
pc=LC->elem;
pa_last=LA.elem[LA.length-1];
pb_last=LB.elem[LB.length-1];
while((pa<=pa_last)&&(pb<=pb_last))
{
if(*pa<=*pb)
{
*pc=*pa;
pc++;
pa++;
}
else
{
*pc=*pb;
pb++;
pc++;
}
}
while(pa<=pa_last)
{
*pc=*pa;
pc++;
pa++;
}
while(pb<=pb_last)
{
*pc=*pb;
pc++;
pb++;
}
}
int main()
{
int n,a,b,i,j,*e;
SqList *LA,*LB,*LC;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Case #%d:\n",i);
scanf("%d",&a);
GetElem(LA,a,e);
scanf("%d",&b);
GetElem(LB,b,e);
}
}
调试了几次感觉是内存分配的错误,到底是哪里错了应该怎么改啊。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询