typedef struct {
int* element;
int length;
}Sqlist;
Sqlist Inittable(Sqlist L) {
L.element = (int *)malloc(sizeof(Sqlist));
if (L.element == NULL) {
exit(1);
}
L.length = 0;
return L;
}
void show(Sqlist L) {
for (int i = 0; i < L.length; i++) {
printf("%d ", L.element[i]);
}
printf("\n");
}
Sqlist conmine(Sqlist A, Sqlist B, Sqlist C) {
int* pa, * pb,*pc;
pa = A.element;
pb = B.element;
C.length = A.length + B.length;
pc = C.element;
int* pa_last, * pb_last;
pa_last = A.element + A.length - 1;
pb_last = B.element + B.length - 1;
while(pa <= pa_last && pb <= pb_last){
if (*pa <= *pb) {
*pc++ = *pa++;
}
else {
*pc++ = *pb++;
}
}
while (pa <= pa_last)
*pc++ = *pa++;//LB已经到头了,将LA中的剩余元素插入到LC中
while (pb <= pb_last)
*pc++ = *pb++;//LA已经到头了,将LB中的剩余元素插入到LC中
return C;
}
int main(void) {
Sqlist A, B, C;
A.element = (int*)malloc(sizeof(Sqlist));
B.element = (int*)malloc(sizeof(Sqlist));
C.element = (int*)malloc(sizeof(Sqlist));
A=Inittable(A);
B=Inittable(B);
C=Inittable(C);
int nums[3] = { 1,7,8 };
for (int i = 0; i < 3; i++) {
A.element[i] = nums[i];
A.length++;
}
int num[6] = { 2,4,6,8,9,10 };
for (int i = 0; i < 6; i++) {
B.element[i] = num[i];
B.length++;
}
show(A);
show(B);
C = conmine(A, B, C);
show(C);
return 0;
}
03-12
797
03-14
3626