/* ECP: FILEname=fig10_12.c */ /* 1*/ #include /* 2*/ #include /* 3*/ typedef struct /* 4*/ { /* 5*/ int *Array; /* 6*/ int Size; /* 7*/ } SortedInts; /* 1*/ /* Merge Two Sorted Lists. Since SortedInts Is */ /* 2*/ /* A Small Structure We Avoid Pointers */ /* 3*/ SortedInts /* 4*/ MergeTwo( SortedInts struct1, SortedInts struct2 ) /* 5*/ { /* 6*/ int *C; /* 7*/ int *A = struct1.Array, *B = struct2.Array; /* 8*/ int ACtr = 0, BCtr = 0, CCtr = 0; /* 9*/ C = malloc( sizeof( int ) * ( struct1.Size + struct2.Size ) ); /*10*/ if( C == NULL ) /*11*/ { /*12*/ printf( "Can not allocate temporary array\n" ); /*13*/ return struct1; /*14*/ } /*15*/ while( ACtr < struct1.Size && BCtr < struct2.Size ) /*16*/ if( A[ ACtr ] <= B[ BCtr ] ) /*17*/ C[ CCtr++ ] = A[ ACtr++ ]; /*18*/ else /*19*/ C[ CCtr++ ] = B[ BCtr++ ]; /*20*/ while( ACtr < struct1.Size ) /*21*/ C[ CCtr++ ] = A[ ACtr++ ]; /*22*/ while( BCtr < struct2.Size ) /*23*/ C[ CCtr++ ] = B[ BCtr++ ]; /*24*/ free( A ); free( B ); /*25*/ struct1.Size += struct2.Size; /*26*/ struct1.Array = C; /*27*/ return struct1; /*28*/ }