DS sequence tables merge two non-decreasing linear tables, ds linear
As early as I wrote a linear table, I mentioned this complicated operation. Today we will merge it.
It is known that the data elements in the order table La and Lb are arranged in a non-descending order by value. Now we need to combine La AND Lb into a new order table Lc, the data elements in Lc are arranged in a non-descending order by values.
Example: La = (3,5, 8,11)
Lb = (, 15, 20)
Then Lc = (, 20)
To implement merging, the basic operations of the sequence table are as follows: Prepare before 0 basic operations, 1 initialize the sequence table, 6 insert data elements to the sequence table, and the Merge function you write, finally, all data elements of Lc must be input and output in the main function.
Merge function code:
<Span style = "font-size: 18px;"> // Merge function void MergeList (SqList La, SqList Lb, SqList & Lc) {ElemType * pa = La. elem; ElemType * pb = Lb. elem; Lc. listsize = Lc. length = La. length + Lb. length; ElemType * pc = Lc. elem = (ElemType *) malloc (Lc. listsize * sizeof (ElemType); // if (! Lc. elem) {exit (OVERFLOW);} ElemType * pa_last = La. elem + La. length-1; ElemType * pb_last = Lb. elem + Lb. length-1; while (pa <= pa_last & pb <= pb_last) // merge {if (* pa <= * pb) {* pc ++ = * pa ++;} else {* pc ++ = * pb ++;} while (pa <= pa_last) // Insert the remaining elements in La {* pc ++ = * pa ++;} while (pb <= pb_last) // Insert the remaining elements in Lb {* pc ++ = * pb ++ ;}}</span>
The complete code for merging is:
<Span style = "font-size: 18px;" >#include <iostream> using namespace std; # include <malloc. h> # include <stdlib. h> // 0 prerequisites # define TRUE 1 # define FALSE 0 # define OK 1 # define ERROR 0 # define OVERFLOW-2 # define LIST_INIT_SIZE 100 # define LISTINCREMENT 10 typedef int elemType; typedef int Status; typedef struct {ElemType * elem; // The base address of the bucket int length; // The current length int listsize; // the size of the currently allocated storage} SqList; // defines a struct type and name it Sqlist // 1 initialization order Table Status InitList (SqList & L) {L. elem = (ElemType *) malloc (LIST_INIT_SIZE * sizeof (ElemType); if (! L. elem) {exit (OVERFLOW);} L. length = 0; L. listsize = LIST_INIT_SIZE; return OK;} // 6 insert the data element Status ListInsert (SqList & L, int I, ElemType e) to the sequence table) {if (I <1 | I> L. length + 1) {return ERROR;} if (L. length> = L. listsize) {ElemType * newbase = (ElemType *) realloc (L. elem, (L. listsize + LISTINCREMENT) * sizeof (ElemType); if (! Newbase) {exit (OVERFLOW);} L. elem = newbase; L. listsize + = LISTINCREMENT;} ElemType * q = & (L. elem [I-1]); ElemType * p; for (p = & (L. elem [L. length-1]); p> = q; p --) {* (p + 1) = * p;} * q = e; ++ L. length; return OK;} // The void MergeList (SqList La, SqList Lb, SqList & Lc) {ElemType * pa = La. elem; ElemType * pb = Lb. elem; Lc. listsize = Lc. length = La. length + Lb. length; ElemType * pc = Lc. elem = (ElemType *) malloc (Lc. listsize * sizeof (ElemType ); // If (! Lc. elem) {exit (OVERFLOW);} ElemType * pa_last = La. elem + La. length-1; ElemType * pb_last = Lb. elem + Lb. length-1; while (pa <= pa_last & pb <= pb_last) // merge {if (* pa <= * pb) {* pc ++ = * pa ++;} else {* pc ++ = * pb ++;} while (pa <= pa_last) // Insert the remaining elements in La {* pc ++ = * pa ++;} while (pb <= pb_last) // Insert the remaining elements in Lb {* pc ++ = * pb ++;} int main () {SqList La, Lb, Lc; // declare La, Lb, and LC; InitList (La); InitList (Lb); cout <"Enter the number of data in La:"; int na; ElemType e; cin> na; for (int I = 1; I <= na; I ++) {cin> e; ListInsert (La, I, e );} cout <"Enter the number of data in Lb:"; int nb; cin> nb; for (I = 1; I <= nb; I ++) {cin> e; ListInsert (Lb, I, e);} MergeList (La, Lb, Lc); for (I = 0; I <na + nb; I ++) {cout <Lc. elem [I] <"," ;}cout <endl; return 0 ;}</span>
Input data: La = (2, 5, 6)
Lb = (3,5, 7,9, 11)
The output result is: