[Data structure] ordered linear table

Source: Internet
Author: User

I am looking for a job now. I have to review the data structure for some time. I would like to take some review notes here. I would like to share with readers, and I would like to encourage myself to stick to it.

The linear table structure of sequential storage is defined as follows:

Typedef struct {<br/> ElemType * elem; <br/> int length; <br/> int listsize; <br/>} SqList;

First, you must apply for a continuous bucket with a specified length of 0.

// Initialize a linear table <br/> Status InitList (SqList * L) <br/>{< br/> (* L ). elem = (ElemType *) malloc (LIST_INIT_SIZE * sizeof (ElemType); <br/> if (! (* L ). elem) <br/> exit (OVERFLOW); <br/> (* L ). length = 0; <br/> (* L ). listsize = LIST_INIT_SIZE; <br/> return OK; <br/>}

To destroy a linear table, you must release the storage space and assign the pointer to elem to avoid being a wild pointer.

// Destroy the linear table <br/> Status DestroyList (SqList * L) <br/>{< br/> free (* L ). elem); <br/> (* L ). elem = NULL; // the pointer must be NULL to avoid the occurrence of a wild pointer <br/> (* L ). length = 0; <br/> (* L ). listsize = 0; <br/> return OK; <br/>}

To clear a linear table, you only need to change the length to 0.

// Clear the linear table <br/> Status ClearList (SqList * L) <br/>{< br/> (* L ). length = 0; <br/> return OK; <br/>}

Determines whether the linear table is empty.

// Determine whether the linear table is empty <br/> Status ListEmpty (SqList L) <br/>{< br/> return L. length = 0? TRUE: FALSE; <br/>}

Returns the length of a linear table.

// Return the length of the linear table <br/> Status ListLength (SqList L) <br/>{< br/> return L. length; <br/>}

To obtain the data element at the specified position, you only need to add **-1 to the base address of the bucket.

// Retrieve the element at the specified position of the linear table <br/> Status GetElem (SqList L, int n, ElemType * e) <br/>{< br/> if (n> L. length | n <1) <br/> exit (ERROR); <br/> else <br/>{< br/> * e = L. elem [n-1]; <br/>}< br/> return OK; <br/>}

Queries the location of a specified element.

// Query the location of a specified Element <br/> int LocateElem (SqList L, ElemType e, Status (* compare) (ElemType, ElemType )) <br/>{< br/> ElemType * p; <br/> int I = 1; <br/> p = L. elem; <br/> while (I <= L. length & compare (* p ++, e) <br/>{< br/> ++ I; <br/>}< br/> if (I <= L. length) <br/> return I; <br/> else <br/> return 0; <br/>}< br/> // implement the compare function <br/> Status compare (int a, int B) <br/>{< br/> return (a-B); <br/>}

Returns the precursor of a specified element.

Status PriorElem (SqList L, ElemType cur_e, ElemType * pre_e) <br/>{< br/> int I = 2; // except for the first element, other elements have only one precursor <br/> ElemType * p = L. elem + 1; <br/> while (I <= L. length & p! = Cur_e) <br/>{< br/> p ++; <br/> I ++; <br/>}< br/> if (I> L. length) <br/>{< br/> return INFEASIBLE; <br/>}< br/> else <br/>{< br/> * pre_e = * -- p; <br/> return OK; <br/>}< br/>}

Returns the successor of a specified element.

// Return the successor of the specified Element <br/> Status NextElem (SqList L, ElemType cur_e, ElemType * next_e) <br/>{< br/> int I = 1; <br/> Elem * p = L. elem; <br/> while (I <L. length & p! = Cur_e) <br/>{< br/> I ++; <br/> p ++; <br/>}< br/> if (I = L. length) <br/> return INFEASIBLE; <br/> else <br/>{< br/> * next_e = * ++ p; <br/> return OK; <br/>}< br/>

Insert an element at a specified position

Status ListInsert (SqList * L, int I, ElemType e) <br/>{< br/> ElemType * newbase, * q, * p; <br/> if (I <1 | I> (* L ). length + 1) <br/>{< br/> return ERROR; <br/>}< br/> if (* L ). length> = (* L ). listsize) <br/>{< br/> newbase = (ElemType *) realloc (* L ). elem, (* L ). listsize + LISTINCREMENT) * sizeof (ElemType); <br/> if (newbase) <br/> exit (OVERFLOW); <br/> (* L ). elem = newbase; <br/> (* L ). listsize + = LISTINCREMENT; <br/>}< br/> q = (* L ). elem + I-1; <br/> for (p = (* L ). elem + (* L ). length-1; p> = q; -- p) <br/> * (p + 1) = * p; // move the element position <br/> * q = e; <br/> ++ (* L ). length; <br/> return OK; <br/>}

Deletes an element at a specified position.

Status ListDelete (SqList * L, int I, ElemType * e) <br/>{< br/> ElemType * p, * q; <br/> if (I <1 | I> (* L ). length) <br/> return ERROR; <br/> p = (* L ). elem + I-1; <br/> * e = * p; <br/> q = (* L ). elem + (* L ). length-1; <br/> for (+ p; p <= q; ++ p) <br/> {<br/> * (p-1) = * p; // move the elements behind the position in sequence <br/>}< br/> (* L ). length --; <br/> return OK; <br/>}

To traverse the elements in a linear table, visit calls print

Status ListTraverse (Status L, void (* visit) (ElemType *) <br/>{< br/> ElemType * p; <br/> int I; <br/> p = L. elem; <br/> for (I = 1; I <= L. length; I ++) <br/>{< br/> visit (p ++); <br/>}< br/> printf ("/n "); <br/> return OK; <br/>}< br/> void print (ElemType * c) <br/>{< br/> printf ("% d ", * c); <br/>}

Merge two linear tables

Void MergeList (SqList La, SqList Lb, SqList * Lc) <br/>{< br/> int I = 1, j = 1, k = 0; <br/> int La_len, Lb_len; <br/> ElemType ai, bj; <br/> Init (Lc); <br/> La_len = ListLength (La ); <br/> Lb_len = ListLength (Lb); <br/> while (I <= La_len & j <= Lb_len) <br/>{< br/> GetElem (La, I, & ai); <br/> GetElem (Lb, j, & bj ); <br/> if (ai <= bj) <br/>{< br/> ListInsert (Lc, ++ k, ai); <br/> ++ I; <br/>}< br/> else <br/> {<br/> ListInsert (Lc, ++ k, bj); <br/> ++ j; <br/>}< br/> while (I <= La_len) <br/>{< br/> GetElem (La, I ++, & ai); <br/> ListInsert (Lc, ++ k, ai); <br/>}< br/> while (j <= Lb_len) <br/>{< br/> GetElem (Lb, j ++, & bj); <br/> ListInsert (Lc, ++ k, bj ); <br/>}< br/>}

With so many basic operations, it seems that the linear table stored in sequence is relatively simple. The final merge is a bit difficult.

 

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.