#include <stdio.h>#include<malloc.h>#defineOK 1#defineOVERFLOW-1#defineERROR 0#defineList_init_size 100#defineListincrement 10typedefintElemtype;typedefintStatus;typedefstruct{elemtype*Elem; intlength; intListsize;} SqList;//////////////////////////InitializeStatus initlist_sq (SqList *L) {L->elem = (Elemtype *)malloc(List_init_size *sizeof(Elemtype)); if(! L->elem)returnOVERFLOW; L->length =0; L->listsize =list_init_size; returnOK;}//////////////////////////InsertStatus listinsert_sq (sqlist * L,intI, Elemtype e) {Elemtype* newbase, * p, *Q; if(I <1|| I > L->length +1) returnERROR; if(L->length >= l->listsize) {Newbase= (Elemtype *)realloc(L->elem, (l->listsize + listincrement) *sizeof(Elemtype)); if(!newbase)returnOVERFLOW; L->elem =newbase; L->listsize = L->listsize +listincrement; } q= & (l->elem[i-1]); for(p = & (l->elem[l->length-1]); p>=q; --p)* (p+1) = *p; *q =e; ++l->length; returnOK;}//////////////////////////Enter values into tablevoidInput_data (SqList *L) { intI, n=5; Elemtype e; printf ("Please enter data for the sequential table: \ n"); for(i=1; i<=n; i++) {printf ("Please enter%d data:", i); scanf ("%d", &e); if(!listinsert_sq (l,i,e)) {printf ("Assignment failed!! "); Break; }} printf ("input Complete! "); printf ("The data for table LA is: \ n"); for(i =1; I <= l->length; i++) printf ("e[%d]=%d\n", I-1, l->elem[i-1]); printf ("Length:%d\n\n", l->length);}////////////////////////////////MergerStatus mergelist (sqlist * la,sqlist * lb,sqlist *Lc) { int* PA, * pa_last, * pb, * pb_last, *pc; PA= la->Elem; PB= lb->Elem; Lc->listsize = Lc->length = La->length + lb->length; PC= Lc->elem = (Elemtype *)malloc(lc->listsize*sizeof(int)); if(! Lc->elem)returnOVERFLOW; Pa_last= La->elem + La->length-1; Pb_last= Lb->elem + Lb->length-1; while(PA <=pa_last)*pc++ = *pa++; while(Pb <=pb_last)*pc++ = *pb++; returnOK;}//////////////////////////////////Main functionintMainvoid) {sqlist La, Lb, Lc; inti; Elemtype e; printf ("/ *-------------merge operation----------------*\n"); if(INITLIST_SQ (&La)) {printf ("LA initialization succeeded \ n"); printf ("la length:%d\n", la.length); printf ("la capacity:%d\n\n", la.listsize); } Elseprintf ("LA initialization error!"); printf ("Please enter the data in LA \ n"); Input_data (&La); printf ("input complete \ n"); if(INITLIST_SQ (&Lb)) {printf ("lb initialization succeeded \ n"); printf ("lb Length:%d\n", la.length); printf ("lb Capacity:%d\n\n", la.listsize); } Elseprintf ("lb initialization error!"); printf ("Please enter the data in LB \ n"); Input_data (&Lb); if(Mergelist (&la, &LB, &Lc)) printf ("Merge succeeded \ n"); for(i =1; I <= lc.length; i++) printf ("e[%d]=%d\n", I-1, lc.elem[i-1]); printf ("Length:%d\n", lc.length); return 0;}
Data structure on-Machine 1 sequence table