题目:已知线性表La和Lb中的数据元素按值非递减有序排列,现要将两表归并为新表Lc,使Lc中所有元素仍然按值非递减有序排列,其中La=(3,5,8,11),Lb=(2,6,8,9,11,15,20),Lc=(2,3,5,6,8,8,9,11,11,15,20)。
问题:请问我这个代码哪里错了,能改正并且说明错误原因吗?
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define Status int
#define ElemType int
//顺序表结构
typedef struct {
ElemType* data;
int length;
}SqList;
//初始化
void InitList(SqList& L)
{
L.data = new ElemType[MAXSIZE];
L.length = 0;
}
//创建线性表
void Create(SqList& L)
{
int n, i;
printf("请输入要输入元素的个数:");
scanf_s("%d", &n);
if (n<0 || n>MAXSIZE)
{
printf("请输入正确的个数!\n");
}
for (i = 0; i < n; i++)
{
printf("请输入第%d个元素:", (i + 1));
scanf_s("%d", &(L.data[i]));
L.length++;
}
}
//打印线性表
void PrintList(SqList L)
{
printf("所有元素:");
for (int i = 0; i < L.length; i++)
{
printf("%d ", L.data[i]);
}
printf("\n");
}
void MergeList(SqList& La, SqList& Lb, SqList& Lc)
{
int i;
int m = La.length;
int n = Lb.length;
int p = Lc.length;
int q;
if (m > n)
q = n;
else
q = m;
for (i = 0; i < m + n; i++)
{
if (i <= q)
{
if (La.data[i] <= Lb.data[i])
{
Lc.data[p] = La.data[i];
Lc.data[p + 1] = Lb.data[i];
Lc.length = Lc.length + 2;
p=p+2;
}
if (La.data[i] > Lb.data[i])
{
Lc.data[p] = Lb.data[i];
Lc.data[p + 1] = La.data[i];
Lc.length = Lc.length + 2;
p=p+2;
}
}
if (i > q)
{
if (m > n)
{
Lc.data[p] = La.data[i];
Lc.length++;
p++;
}
if (m < n)
{
Lc.data[p] = Lb.data[i];
Lc.length++;
p++;
}
}
}
}
int main()
{
SqList La, Lb, Lc;
//初始化
InitList(La);
InitList(Lb);
InitList(Lc);
//创建
printf("请输入集合La的元素:\n");
Create(La);
printf("请输入集合Lb的元素:\n");
Create(Lb);
//合并A和B集合到Lc中
MergeList(La, Lb,Lc);
PrintList(Lc);
}