# Include <iostream> Using namespace STD;# Define maxsize 20 Typedef int elemtype; Typedef struct { Elemtype data [maxsize]; Int length; } Sqlist; /* Initialize the ordered linear table */ Bool initlist (sqlist * PTR) { For (INT I = 0; I <maxsize; I ++) PTR-> data [I] = 0; PTR-> length = 0; Return true; } Bool listempty (sqlist sq) { If (sq. Length = 0) Return true; Else Return false; } Bool clearlist (sqlist * PTR) { For (INT I = 0; I <PTR-> length; I ++) PTR-> data [I] = 0; PTR-> length = 0; Return true; } /* Use PTR to return the value of the second POS data element in sq. Note that POS refers to the position, and the array of the second position starts from 0 */ Bool getelem (sqlist SQ, int POs, elemtype * PTR) { If (sq. Length = 0 | POS <1 | POS> sq. length) Return false; * PTR = SQ. Data [pos-1]; Return true; } /* Return the order of the 1st data elements that meet the ELEM requirements in sq. If such a data element does not exist, the return value is 0 */ Int locate (sqlist SQ, elemtype ELEM) { For (INT I = 0; I <sq. length; I ++) { If (sq. Data [I] = ELEM) Return I + 1; } Return 0; } /* Insert a new data element ELEM before the POs position in sq. The length of L is increased by 1 */ Bool listinsert (sqlist * PTR, int POs, elemtype ELEM) { If (PTR-> length = maxsize)/* The ordered linear table is full */ Return false; If (Pos <1 | POS> PTR-> Length + 1) Return false; If (Pos <= PTR-> length) { /* Move one data element after the position to be inserted to the backend */ For (INT I = PTR-> length-1; I> = pos-1; I --) { PTR-> data [I + 1] = PTR-> data [I]; } } PTR-> data [pos-1] = ELEM;/* Insert new elements */ PTR-> length ++; Return true; } /* Delete the second POS data element of PS and return its value with PE. The length of PS is reduced by 1 */ Bool listdelete (sqlist * ps, int POs, elemtype * PE) { If (Pos <1 | POS> PS-> length) Return false; * Pe = ps-> data [pos-1]; /* Move the deleted position successor element forward */ For (INT I = Pos; I <ps-> length; I ++) PS-> data [I-1] = ps-> data [I]; PS-> length --; Return true; } Int listlength (sqlist sq) { Return sq. length; } /* Insert all the elements in the Pb but not in the PA of the linear table into the PA */ Void unionlist (sqlist * pA, sqlist * pb) { Int Lena = pa-> length; Int lenb = Pb-> length; Int item; For (INT I = 0; I <lenb; I ++) { If (getelem (* pb, I + 1, & item )) { If (locate (* pA, item) = 0) Listinsert (Pa, ++ Lena, item ); } } } Int main (void) { Sqlist sq; Initlist (& sq ); For (INT I = 1; I <5; I ++) Listinsert (& SQ, I, I ); If (! Listempty (SQ )) { Cout <"SQ:" <Endl; For (INT I = 0; I <listlength (SQ); I ++) Cout <sq. Data [I] <''; } Cout <Endl; Int Pos = locate (SQ, 2 ); If (Pos! = 0) { Int result; Listdelete (& SQ, POs, & result ); Cout <"delete:" <result <Endl; } If (! Listempty (SQ )) { Cout <"SQ:" <Endl; For (INT I = 0; I <listlength (SQ); I ++) Cout <sq. Data [I] <''; } Cout <Endl; Sqlist sq2; Initlist (& sq2 ); For (INT I = 1; I <4; I ++) Listinsert (& sq2, I, 6 ); Listinsert (& sq2, 4, 7 ); If (! Listempty (sq2 )) { Cout <"sq2:" <Endl; For (INT I = 0; I <listlength (sq2); I ++) Cout <sq2.data [I] <''; } Cout <Endl; Unionlist (& SQ, & sq2 ); If (! Listempty (SQ )) { Cout <"SQ:" <Endl; For (INT I = 0; I <listlength (SQ); I ++) Cout <sq. Data [I] <''; } Cout <Endl; Return 0; } |