求高手解关于线性表的问题
#include<stdio.h>#include<stdlib.h>
#include<conio.h>
#define LIST_INIT_SIZE 5
#define LISTINCREMENT 1
typedef struct{
int *elem;
int length;
int listsize;
}SqList;
SqList Init(SqList L); //初始化函数
SqList Input(SqList L); //输入LA 和 LB 顺序表数据
void Output(SqList L); //输出函数
SqList Merge(SqList LA,SqList LB,SqList LC);
SqList Renew(SqList LC); //此函数为分配LC顺序表增量
int main(void)
{
SqList LA,LB,LC;
LA=Init(LA);
LB=Init(LB);
LA=Input(LA);
LB=Input(LB);
Output(LA);
Output(LB);
LC=Merge(LA,LB,LC);
Output(LC);
getch();
return 0;
}
SqList Init(SqList L)
{
L.elem=(int *)malloc(LIST_INIT_SIZE * sizeof(int));
if(!L.elem)
{
printf("Dynamic memory malloc failure.\n");
exit(1);
}
L.length=0;
L.listsize=LIST_INIT_SIZE;
return L;
}
SqList Input(SqList L)
{
int value;
int *newbase;
printf("Please input value: ");
scanf("%d",&value);
while(value)
{
L.elem[L.length]=value;
L.length++;
if(L.length>=L.listsize)
{
newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT) * sizeof(int));
if(!newbase)
{
printf("Renew malloc memory failure.\n");
exit(1);
}
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
scanf("%d",&value);
}
return L;
}
void Output(SqList L)
{
int i;
for(i=0;i<=L.length-1;i++)
printf("%d\t",L.elem[i]);
free(L.elem);
printf("\n");
}
SqList Merge(SqList LA,SqList LB,SqList LC) //主要就是此函数
{
int i,j;
LC=Init(LC);
i=0;
j=0;
while(i<=LA.length-1 && j<=LB.length-1)
{
if(LA.elem[i]<=LB.elem[j])
{
LC.elem[LC.length]=LA.elem[i];
LC.length++;
i++;
if(LC.length>=LC.listsize)
LC=Renew(LC);
}
else
{
LC.elem[LC.length]=LB.elem[j];
LC.length++;
j++;
if(LC.length>=LC.listsize)
LC=Renew(LC);
}
}//while
while(i<=LA.length-1)
{
LC.elem[LC.length]=LA.elem[i];
LC.length++;
i++;
if(LC.length>=LC.listsize)
LC=Renew(LC);
}
while(j<=LB.length-1)
{
LC.elem[LC.length]=LB.elem[j];
LC.length++;
j++;
if(LC.length>=LC.listsize)
LC=Renew(LC);
}
return LC;
}
SqList Renew(SqList LC)
{
int *newbase;
newbase=(int *)realloc(LC.elem,(LC.listsize+LISTINCREMENT) * sizeof(int));
if(!newbase)
{
printf("Renew malloc memory failure.\n");
exit(1);
}
LC.elem=newbase;
LC.listsize+=LISTINCREMENT;
return LC;
}
这是一个线性表合并的问题,我想问的是线性表是不是就是数组,那为什么还用结构体定义,直接定义两个数组然后和合并不就行了吗?