用C语言合并两个线性表
时间: 2023-05-14 16:05:50 浏览: 126
数据结构考研线性表
可以使用以下代码来合并两个线性表:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
void MergeList(SqList La, SqList Lb, SqList *Lc) {
int i = 0, j = 0, k = 0;
while (i < La.length && j < Lb.length) {
if (La.data[i] <= Lb.data[j]) {
Lc->data[k++] = La.data[i++];
} else {
Lc->data[k++] = Lb.data[j++];
}
}
while (i < La.length) {
Lc->data[k++] = La.data[i++];
}
while (j < Lb.length) {
Lc->data[k++] = Lb.data[j++];
}
Lc->length = k;
}
int main() {
SqList La = {{1, 3, 5, 7, 9}, 5};
SqList Lb = {{2, 4, 6, 8, 10}, 5};
SqList Lc;
MergeList(La, Lb, &Lc);
for (int i = 0; i < Lc.length; i++) {
printf("%d ", Lc.data[i]);
}
printf("\n");
return 0;
}
阅读全文