Sequential storage linear table

Source: Internet
Author: User
/*** Data Structure <1> * Implementation of sequential storage linked list */# include <iostream> using namespace STD; typedef int elemtype; # define list_init_size 100 // initial allocation of a linear table storage space # define listincrement 10 // incremental typedef struct {elemtype * ELEM; // base address of the bucket, ELEM is the int length pointer to the int type; // It stores the int listsize of the current linear table; // The currently allocated storage capacity (in sizeof (elemtype)} sqlist; // construct an empty linear table int initlist (sqlist & L) {L. ELEM = (elemtype *) malloc (list_init_size * sizeof (elemtype); If (! L. ELEM) return 0; L. listsize = list_init_size; L. length = 0; return 1 ;}// destroy the linear table void destroylist (sqlist & L) {If (L. ELEM! = NULL) {free (L. ELEM); L. ELEM = NULL; cout <"linear table destroyed successfully! "<Endl ;}// reset L to an empty table void clearlist (sqlist & L) {If (L. ELEM! = NULL) {L. Length = 0; cout <"linked list empty! "<Endl ;}// if l is empty, true is returned; otherwise, falseint listempty (sqlist L) {If (L. ELEM) {If (L. length = 0) {cout <"Empty linked list" <Endl; return 1 ;}elsecout <"non-empty linked list" <Endl ;}return 0 ;} // output the number of data elements in L int listlength (sqlist L) {If (L. ELEM) {// cout <"the number of data elements in the table is:" <L. length <Endl; return L. length;} return 0;} // use e to return the int getelem (sqlist L, int I, elemtype & E) {If (L. ELEM) if (I> = 0 & I <= listlength (L) {e = L. ELEM [I-1]; // The Position of the I-th Data Element in the table is the I-1 // cout <"Table No." <I <"data element is" <e <Endl; Return e;} return 0 ;} // return the order of the first 1st data elements in the same relationship with E. If such an element does not exist, the return value is void locateelem (sqlist L, elemtype e) {int I; If (L. ELEM) {If (L. length! = 0) {for (I = 0; I <L. length; I ++) {If (L. ELEM [I] = E) {cout <"searched element" <e <"appears in the" <I + 1 <"position" <Endl ;} }}}} // If cur_e is a data element of L and is not the first, use pre_e to return its precursor. Otherwise, the operation fails. pre_e does not define void priorelem (sqlist L, elemtype cur_e, elemtype & pre_e) {int I; If (L. ELEM) {If (L. length) {for (I = 0; I <L. length; I ++) {If (L. ELEM [I] = cur_e & I = 0) {cout <"this location element has no precursor" <Endl;} If (L. ELEM [I] = cur_e & I! = 0) // The first element does not have a precursor {pre_e = L. ELEM [I-1]; cout <"Data Element" <I <"precursor is" <pre_e <Endl ;}}}}} // If cur_e is a data element of L and is not the last one, use next_e to return its successor; otherwise, the operation fails. next_e does not define void nextelem (sqlist L, elemtype cur_e, elemtype & next_e) {int I; If (L. ELEM) {If (L. length) {for (I = 0; I <L. length; I ++) {If (L. ELEM [I] = cur_e & I = L. length-1) {cout <"this location element has no successor" <Endl;} If (L. ELEM [I] = cur_e & I! = L. length-1) {next_e = L. ELEM [I + 1]; cout <"Data Element" <I <"is followed by" <next_e <Endl ;}}}}} // Insert a new data element E before position I in L. Add 1 void listinsert (sqlist & L, int I, elemtype e) {elemtype * P, * q, * newbase; If (L. ELEM) if (I> 0 & I <= L. length) if (L. length> = L. listsize) {newbase = (elemtype *) realloc (L. ELEM, (L. listsize + listincrement) * sizeof (elemtype); If (! Newbase) cout <"failed! "<Endl; L. ELEM = newbase; L. listsize + = listincrement;} q = & (L. ELEM [I-1]); // Q is the insert position for (P = & (L. ELEM [L. length-1]); P> = Q; -- p) * (p + 1) = * P; // shift the element after the element is inserted to the right * q = E; // insert element ++ L. length; // increase the table length by 1 // cout <L. length <Endl;} // Delete the I-th data element of L and return its value with E. The length of L is reduced by 1 void listdelete (sqlist & L, int I, elemtype & E) {elemtype * P, * q; If (L. ELEM) if (I> = 0 & I <= L. length) P = & (L. ELEM [I-1]); E = * P; q = L. ELEM + L. length-1; // The Position of the end element of the table. For (+ + P; P <= Q; ++ p) {* P-1) = * P; // move forward after deletion }- -L. length; cout <"deletion successful! "<Endl ;}// call visit () for each data element of L sequentially. Once visit () fails, the operation fails void listtraverse (sqlist L) {int I; if (L. ELEM) {cout <"the elements stored in the sequential storage table are:"; for (I = 0; I <L. length; I ++) cout <L. ELEM [I] <"; cout <Endl;} elsecout <" linear table is empty! "<Endl;} // known linear table LA, the data elements in LB are arranged by values in non-decreasing order // merge la, lb to obtain the new linear table lcvoid mergelist (sqlist la, sqlist LB, sqlist & lc) {int I, j, k = 0; int A [4] = {,}, B [7] =, 20}; int la_len, lb_len; for (I = 1; I <5; I ++) listinsert (LA, I, a [I-1]); cout <"the element of the linear la table is:"; for (I = 0; I <LA. length; I ++) cout <LA. ELEM [I] <"; cout <Endl; For (j = 1; j <8; j ++) listinsert (LB, J, B [J-1]); cout <"linear table LB elements:"; for (j = 0; j <lb. length; j ++) cout <lb. ELEM [J] <"; cout <Endl; initlist (LC); I = j = 1; la_len = La. length; lb_len = LB. length; while (I <= la_len) & (j <= lb_len) {getelem (LA, I, a [I-1]); getelem (LB, J, B [J-1]); if (a [I-1] <= B [J-1]) {listinsert (LC, ++ K, a [I-1]); ++ I ;} else {listinsert (LC, ++ K, B [J-1]); ++ J ;}}while (I <= la_len) {getelem (LA, I, A [I-1]); listinsert (LC, ++ K, a [I-1]); I ++;} while (j <= lb_len) {getelem (LB, J, B [J-1]); listinsert (LC, ++ K, B [J-1]); j ++;} cout <"after merging linear table LC :"; for (I = 0; I <LC. length; I ++) cout <LC. ELEM [I] <"; cout <Endl;} int main () {int I; elemtype J; sqlist L; initlist (l); for (I = 1, j = 0; I <= 10; I ++, J ++) listinsert (L, I, j); // I is the position of the inserted element, J is the inserted data */listtraverse (l);/* // test the mergelist function sqlist la, LB, LC; initlist (LA); initlist (LB); mergelist (LA, LB, LC); * // test the int position and mcout of the listdelete function <"Enter the position of the element to be deleted:" <Endl; CIN> position; listdelete (L, position, m); * // test the priorelem and nextelem functions elemtype pre_e; elemtype next_e; elemtype cur_e; cout <"Enter the element to be searched: "<Endl; CIN> cur_e; priorelem (L, cur_e, pre_e); nextelem (L, cur_e, next_e); * // test the locateelem function elemtype compnum; cout <"Enter the element:"; CIN> compnum; locateelem (L, compnum); * // test the getelem function elemtype ELEM; int K; cout <"Enter the K element to be obtained, K ="; CIN> K; getelem (L, K, ELEM ); * // test the listlength function int Len; Len = listlength (l); * // test the listempty function listempty (l ); * // test the clearlist function clearlist (l); * // test the destroylist function destroylist (l); listtraverse (l); */return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.