# 这个程序是求La和Lb的并集，放到La中，有错误，请帮我看看

0

#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#define list_init_size 100
#define listincrement 5
typedef int ElemType;
typedef struct
{
ElemType * elem;
int length;
int listsize;
}SqList;

int InitList_Sq(SqList & L)
{
L.elem=(ElemType *)malloc(list_init_size*sizeof(ElemType));
if(!L.elem)
{
exit(0);
}
L.length=0;
L.listsize=list_init_size;
return 1;
}

int ListLength(SqList L)
{
return (L.length);
}

int GetElem(SqList L,int i,ElemType & e)
{
if(i<1||i>L.length)
{
return 0;
}
e=L.elem[i-1];
return 1;
}

int LocateElem(SqList L,ElemType e,int (*compare)(ElemType,ElemType))
{
int i=0;
ElemType *p=L.elem;
while(i<=L.length&&!compare(*p++,e))
++i;
if(i<L.length)
return i;
else
return 0;
}

int Sqinsert(SqList &L,int i,ElemType e)
{

ElemType * newbase,j;
// L.length=listsize;
if(i>L.length+1||i<1)
{
return 0;
}
if(L.length>=L.listsize)
{
newbase=(ElemType *)realloc(L.elem,(L.listsize+listincrement)*sizeof(ElemType));
if(!newbase)
{
exit(0);
}
L.elem=newbase;
L.listsize=L.listsize+listincrement;

}
for(j=L.length;j>=i;j--)
{
L.elem[j+1]=L.elem[j];

}
L.elem[i]=e;
L.length++;
return 1;

/* ElemType *newbase,*q,*p;
if(i<1||i>L.length+1)
return 0;
if(L.length==L.listsize)
{ newbase=(ElemType*)realloc(L.elem,(L.listsize)*sizeof(ElemType));
if(!newbase)
exit(0);
L.elem=newbase;
L.listsize+=listincrement;
}
q=L.elem+i-1;
for(p=L.elem+L.length-1;p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return 1;*/
}

void Squnion(SqList & La,SqList Lb)
{
int La_len,Lb_len,i;
ElemType e;
// La_len=ListLength(La);
// Lb_len=ListLength(Lb);

for(i=0;i<=Lb.length ;i++)
{
GetElem(Lb,i,e);
if(!LocateElem(La,e,0))Sqinsert(La,++La_len,e);
}

}
void main()
{

SqList La,Lb;
InitList_Sq(La);
int t;
for(t=1;t<6;t++)
{
Sqinsert(La,t,t);
}
printf("La线性表的数据：La=");
for(t=1;t<=La.length;t++)
{
printf("%d ",La.elem[t]);
}
printf("\n");

InitList_Sq(Lb);
for(t=1;t<6;t++)
{
Sqinsert(Lb,t,t*t);
}
printf("Lb线性表的数据：Lb=");
for(t=1;t<=Lb.length;t++)
{
printf("%d ",Lb.elem[t]);
}
printf("\n");

Squnion(La,Lb);

printf("La线性表的数据：La=");
for(t=1;t<=La.length;t++)
{
printf("%d ",La.elem[t]);
}
printf("\n");

}

3

La_len 这个变量在使用之前没有赋值。貌似还有其他变量使用之前也没赋值

您需要登录以后才能回答，未注册用户请先注册