This article was written by referring to the data structure of the national excellent course Geng Guohua, and found some problems in the book, and made some simple corrections to supplement the entire process. If there are any deficiencies, please follow the instructions !!!
# Include <iostream >#include <cstdio> using namespace STD; # define maxsize 100 # define error 0 # define OK 1 typedef int elemtype; typedef struct {elemtype * ELEM; int last;} sqlist; int locate (sqlist * l, elemtype e) {int I = 0;/* I is the scanning counter, the initial value is 0, start from the first element. */while (I <= L-> last) & (L-> ELEM [I]! = E)/* scan the table sequentially until the element with the value of E is found or the end of the table is scanned but not */I ++ is found; if (I <= L-> last) Return (I + 1);/* If an element with a value of E is found, its serial number */elsereturn (-1) is returned ); /* if no value is found, an empty Sequence Number */}/* is returned, and an element e is inserted before the I Data Element in the sequence table L. The length of the table before insertion is n = L-> last + 1. The valid value range of I is 1 ≤ I ≤ L-> last + 2 */INT inslist (sqlist * l, int I, elemtype e) {int K; If (I <1) | (I> L-> last + 2 )) /* first determine whether the insert position is valid */{printf ("the insert position I value is invalid"); Return (error);} If (L-> last> = MAXSIZE-1) {printf ("the table is full and cannot be inserted"); Return (error) ;}for (k = L-> last; k> = I-1; k --) /* move position for element insertion */L-> ELEM [k + 1] = L-> ELEM [k]; L-> ELEM [I-1] = E; /* in the C language array, the subscript of element I is I-1 */L-> last ++; Return (OK);} int dellist (sqlist * l, int I) /* Delete the I-th Data Element in sequence table L. The valid value of I is 1 ≤ I ≤ L. last + 1 */{int K; If (I <1) | (I> L-> last + 1) {printf ("the deletion location is invalid! "); Return (error) ;}for (k = I; k <= L-> last; k ++) l-> ELEM [k-1] = L-> ELEM [k];/* move the following elements in sequence */L-> last --; Return (OK );} void print (sqlist * l) {int I; for (I = 0; I <= L-> last; I ++) cout <L-> ELEM [I] <""; cout <Endl;} void Init (sqlist * l) {L-> ELEM = new elemtype [maxsize]; if (! L-> ELEM) {exit (0);} int N, I = 0; cout <"Enter the number of elements in the sequence table" <Endl; CIN> N; while (I <n) {CIN> L-> ELEM [I ++];} l-> last = I-1;} void Merge (sqlist * La, sqlist * LB, sqlist * 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) /* When the table la has residual elements, the remaining elements of the table la are assigned to the table LC */{LC-> ELEM [k] = La-> ELEM [I]; I ++; k ++;} while (J <= LB-> last)/* When the LB table has the remaining elements, then, the remaining LB elements of the table are assigned to the LC */{LC-> ELEM [k] = LB-> ELEM [J]; j ++; k ++;} table ;} LC-> last = La-> last + lB-> last + 1; print (LC);} int main () {sqlist L, L1, L2; elemtype E; int I, T; cout <"sequence table operation sequence: (1. initialize 2. search 3. insert 4. delete) "<Endl; Init (& L); print (& L);/* cout <" Enter the elements of the sequence table to be searched "<Endl; cin> E; t = locate (& L, e); If (t) cout <"no." <t <! "<Endl; elsecout <" not found! "<Endl; cout <" Enter the elements and locations of the sequence table to be inserted "<Endl; CIN> E> I; t = inslist (& L, i, e); If (t) {cout <"the inserted sequence table is" <Endl; print (& L) ;}elsecout <"insertion failed! "<Endl; */cout <" Enter the location of the elements in the sequence table to be deleted "<Endl; CIN> I; t = dellist (& L, I ); if (t) {cout <"the sequence table after deletion is" <Endl; print (& L) ;}elsecout <"deletion failed! "<Endl; cout <" initialize L1 and L2: "<Endl; Init (& L1); Init (& l2); cout <" L1, after L2 and l are merged, they are: "<Endl; merge (& L1, & L2, & L); Return 0 ;}
Create, initialize, search, delete, insert, and merge sequence tables