线性表的相关操作问题
要求逆序输入两个线性表的内容,然后将两个线性表合为一个,然后删除相同的元素,正序打印出。代码:
#include<stdio.h>
#include<stdlib.h>
#define listinitsize 100
#define OVERFLOW -1
typedef struct sqlist
{
int *a;
int length;
int listsize;
}sqlist;
sqlist la,lb,lc;
/*线性表的定义*/
sqlist initlist()
{
sqlist l;
l.a=(int *)malloc(listinitsize*sizeof(int));
if(!l.a) exit(OVERFLOW);
l.length=0;
l.listsize=listinitsize;
return l;
}
void createlist(sqlist L,int num)
{
int j;
for(j=1;j<=num;j++)
{
scanf("%d",&L.a[j]);
L.length++;
}
}
/*合并两个线性表*/
sqlist addlist(sqlist la,sqlist lb,sqlist lc)
{
int i=1,j=1,k=1;
while(i<la.length&&j<lb.length)
{
if(la.a[i]<=lb.a[j])
{
lc.a[k]=lb.a[j];
j++;
k++;
}
else
{
lc.a[k]=la.a[i];
i++;
k++;
}
}
while(i<=la.length)
{
lc.a[k]=la.a[i];
k++;
i++;
}
while(j<=lb.length)
{
lc.a[k]=lb.a[j];
k++;
j++;
}
lc.length=k-1;
return lc;
}
/*输出线性表*/
void printlist(sqlist l)
{
int i=1;
for(i=1;i<=l.length;i++)
printf("%6d",l.a[i]);
printf("\n");
}
/*删除线性表中相同的元素*/
sqlist delsame(sqlist l)
{
int i,j,k;
if(l.length>0)
{
for(i=1;i<=l.length;i++)
{for(j=i+1;j<=l.length;j++)
if(l.a[j]==l.a[i])
{
for(k=j+1;k<=l.length;k++)
l.a[k-1]=l.a[k];
l.length--;
}
}
}
return l;
}
/*将线性表逆序排列*/
sqlist change(sqlist l)
{
int i,t;
for(i=1;i<=l.length/2;i++)
{
t=l.a[i];
l.a[i]=l.a[l.length-i];
l.a[l.length-i]=t;
}
return l;
}
/*主函数*/
void main()
{
int num,num0;
la=initlist();
la=initlist();
lc=initlist();
printf("please input thr numbers of you want to input La:");
scanf("%d",&num);
printf("\n");
createlist(la,num);
printf("please input the number of you want to input Lb:");
scanf("%d",&num0);
printf("\n");
createlist(lb,num0);
printf("The list of la:\n");
printlist(la);
printf("The list of lb:\n");
printlist(lb);
addlist(la,lb,lc);
delsame(lc);
change(lc);
printf("The list of lc:\n");
printlist(lc);
getch();
}
最后输入的时候出现错误,请各位帮忙看看。