发布时间:2024-05-09 16:01
书中答案使用了3个循环,我将他合并成了一个循环来实现,伪代码如下:
int Combin_List(SqList L1,SqList L2,SqList*L) {
if (L1.Length + L2.Length > MAXSIZE) {
return ERROR;
}
int k = 0, k1 = 0, k2 = 0;
while (1) {
if (L1.elems[k1] <= L2.elems[k2]&&k1elems[k++] = L1.elems[k1++];
L->Length++;
if (k1 == L1.Length) {
L1.elems[k1]= L2.elems[L2.Length-1];
//将顺序表超出的第一个值设为另一个表的最大值,从而保证另一个if语句正常执行至拷贝结束
}
}
if (L1.elems[k1] >= L2.elems[k2] && k2 < L2.Length) {
L->elems[k++] = L2.elems[k2++];
L->Length++;
if (k2 == L2.Length) {
L2.elems[k2] = L1.elems[L1.Length - 1];
}
}
if (L->Length == L1.Length + L2.Length) {
return OK;
}
}
}