设计一个函数 void merge(SeqList LA, SeqList LB, SeqList *LC) , 该函数的功能是将递减顺序表LA和LB和合并到递减顺序表LC中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <stdio.h> #define maxsize 100
typedef struct { int elem[maxsize]; int last; }SeqList;
void merge(SeqList* LA, SeqList* LB, SeqList* LC) { int i, j, k; i = 0; j = 0; k = 0; while (i < LA->last && j < LB->last) { if (LA->elem[i] >= LB->elem[j]) { LC->elem[k] = LA->elem[i]; i++; k++; } else { LC->elem[k] = LB->elem[j]; j++; k++; } }
while (i <= LA->last) { LC->elem[k] = LA->elem[i]; i++; k++; }
while (j <= LA->last) { LC->elem[k] = LB->elem[j]; j++; k++; }
LC->last = LA->last + LB->last + 1; }
int main(int argc, char* argv[]) { SeqList LA, LB, LC; int i;
for (i = 0; i < 10; i++) { LA.elem[i] = 20 - 2 * i; } LA.last = 9;
for (i = 0; i < 8; i++) { LB.elem[i] = 24 - 3 * i; } LB.last = 7;
merge(&LA, &LB, &LC);
for (i = 0; i <= LC.last; i++) { printf("%d ", LC.elem[i]); } printf("\n"); return 0; }
|