c语言顺序表合并顺序表
时间: 2023-10-22 17:06:15 浏览: 164
顺序表合并可以采用类似归并排序的思想,先将两个顺序表中的元素按照大小顺序合并到一个新的顺序表中,再将新的顺序表复制回原来的顺序表中。具体实现可以参考以下代码:
```c
#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 = {{0}, 0};
MergeList(&La, &Lb, &Lc);
for (int i = 0; i < Lc.length; i++) {
printf("%d ", Lc.data[i]);
}
printf("\n");
return 0;
}
```
阅读全文