Data Structure Algorithm

Source: Internet
Author: User

Void union (List & La, List Lb)

// Insert all data elements in the linear Lb but not in La into La.

{

La_len = ListLength (La); Lb_len = ListLength (Lb); // evaluate the length of a linear table

For (I = 1; I <= Lb_len; I ++)

{

GetElem (Lb, I, e); // obtain the I data element in Lb and assign it to e.

If (! LocateElem (La, e, equal) ListInsert (La, ++ La_len, e); // if the same data element as e does not exist in la, insert it

}

}

**************************************** **************************************** **************************************** ***

Void MergeList (List La, List Lb, List & Lc)

{

// Data elements in the known linear tables La and Lb are arranged in non-descending order of values

// Merge La AND Lb to obtain the new linear table Lc. The data elements of Lc are also arranged in descending order of values.

InitList (Lc );

I = j = 1; k = 0;

La_len = ListLength (La); Lb_len = ListLength (Lb );

While (I <= La_len) & (j <= Lb_len) // neither La nor Lb is empty

{

GetElem (La, I, ai); GetElem (Lb, j, bj );

If (ai <= bj)

{

ListInsert (Lc, ++ k, ai );

++ I;

}

Else

{

ListInsert (Lc, ++ k, bj );

++ J;

}

}

While (I <= La_len)

{

GetElem (La, I ++, ai );

ListInsert (Lc, ++ k, ai );

}

While (j <= Lb_len)

{

GetElem (Lb, j ++, bj );

ListInsert (Lc, ++ k, bj );

}

}

**************************************** **************************************** *****************************

3. Construct an empty linear table L

StatusInitList_Sq (SqList & L)

{

L. elem = (ElemType *) malloc (LIST_INIT_SIZE * sizeof (ElemType ));

If (! L. elem) exit (OVERFLOW); // storage allocation failed

L. length = 0; // The length of the empty table is 0.

L. listsize = LIST_INIT_SIZE; // initial storage capacity

Return OK;

}

**************************************** **************************************** **************************************** *

StatusListInsert_Sq (SqList & L, int I, ElemType e)

// Insert a new element e before position I in the ordered linear table L. The valid value of I is 1 <= I <= ListLength_Sq (L) + 1

{

If (I <1 | I> L. length + 1) returnERROR; // The I value is invalid.

If (L. length> = L. listsize) // The current bucket is full and the allocation is increased.

{

Newbase = (ElemType *) realloc (L. elem, (L. listsize + LISTINCREMENT) * sizeof (ElemType ));

If (! Newbase) exit (OVREFLOW); // storage allocation failed

L. elem = newbase; // new base address

L. listsize + = LISTINCREMENT; // increase the storage capacity

}

Q = & (L. elem [I-1]); // q is the insert position

For (p = & (L. elem [L. length-1]); p> = q; -- p)

* (P + 1) = * p; // right shift of the inserted position and subsequent elements

* Q = e; // insert e

+ L. length; // The table length increases by 1.

Return OK;

}

**************************************** **************************************** **************************************** *******

StatusListDelete_Sq (SqList & L, int I, ElemType & e)

{

// Delete the I-th element in the ordered linear table L and return its value with e. The valid value of I is 1 <= I <= ListLength-Sq (L)

If (I <1 | I> L. length + 1) return ERROR; // The I value is invalid.

P = & (L. elem [I-1]); // p is the location of the deleted Element

E = * p; // The value of the deleted element is assigned to e.

Q = L. elem + L. length-1; // position of the end element of the table

For (++ p; p <= q; ++ p)

* (P-1) = * p; // shifts the left of the deleted element.

-- L. length; // The table length minus 1.

ReturnOK;

}

**************************************** **************************************** **************************************** **********************

6. In the ordered linear table L, find the order of the 1st values and the element e that satisfies the compare (). If the result is found, return the order in the L; otherwise, return 0.

IntLocateElem_Sq (SqList L, ElemType e, Status (* compare) (ElemType, ElemType ))

{

I = 1; // The initial value of I is the order of 1st Elements

P = L. elem; // The initial value of p is the storage location of the first element.

While (I <= L. length &&! (* Compare) (* p ++, e ))

++ I;

If (I <= L. length) return I;

Else return 0;

}

**************************************** **************************************** **************************************** ****************

VoidMergeList_Sq (SqList La, SqList Lb, SqList & Lc)

{

// Data elements in the known linear tables La and Lb are arranged in non-descending order of values

// Merge La AND Lb to obtain the new linear table Lc. The data elements of Lc are also arranged in descending order of values.

Pa = La. elem;

Pb = Lb. elem;

Lc. listsize = Lc. length = La. length + Lb. length;

Pc = Lc. elem = (ElemType *) malloc (Lc. listsize * sizeof (ElemType ));

If (! Lc. elem) exit (OVERFLOW); // storage allocation failed

Pa_last = La. elem + La. length-1;

Pb_last = Lb. elem_Lb.length-1;

While (pa <= pa_last & pb <= pb_last)

{

If (* pa <* pb) * pc ++ = * pa ++;

Else * pc ++ = * pb ++;

}

While (pa <= pa_last) * pc ++ = * pa ++;

While (pb <= pb_last) * pc + = * pb ++;

}

**************************************** **************************************** **************************************** ****************

8. Implementation of the GetElem function in a single-chain table

StatusGetElem_L (LinkList L, int I, ElemType & e)

{

L is the header pointer of the single-chain table of the leading node. When the I-th element exists, the value is assigned to e and OK is returned. Otherwise, error is returned.

P = L-> next; j = 1; // initialization. p points to the first node and j is the counter.

While (p & j

{

P = p-> next;

++ J;

}

If (! P | j> I) return ERROR; // the I-th element does not exist.

E = p-> data; // obtain the I-th element.

Return OK;

}

**************************************** **************************************** **************************************** *******

9. insert and delete a single-chain table

* ************ Insert

StatusListInsert_L (LinkList & L, int I, ElemType e)

{

// Insert element e before position I in the single-chain linear table L of the leading Node

P = L; j = 0;

While (p & j

{

P = p-> next; + + j // find the I-1 Node

}

If (! P | j> I) returnERROR; // I is smaller than 1 or greater than the table length

S = (LinkList) malloc (sizeof (LNode); // generate a new node

S-> data = e;

S-> next = p-> next;

P-> next = s;

Return OK;

}

* ********************** Delete

StatusListDelete_L (LinkList & L, int I, ElemType & e)

{

// In the single-chain linear table L of the leading node, delete the I-th element and e returns its value.

P = L; j = 0;

While (p-> next & j

{

P = p-> next;

++ J;

}

If (! (P-> next) | j> i-1) return ERROR; // The deletion location is unreasonable

Q = p-next; p-> next = q-> next; // Delete and release the node

E = q-> data; free (q );

ReturnOK;

}

**************************************** **************************************** **************************************** **********

11. Create a single-chain table reversely from the end of the table to the header

Void CreateList_L (LinkList & L, int n)

{

// Enter the values of n elements in the reverse order to create a single-chain linear table with the table header node L

L = (LinkList) malloc (sizeof (LNode ));

L-> next = NULL; // create a single-chain table with the leading Node

For (I = n; I> 0; ++ I)

{

P = (LinkList) malloc (sizeof (LNode); // generate a new node

Scanf (& p-> data); // input element value

P-> next = L-> next; L-next = p; // insert to the header

}

}

**************************************** ******************************** To be continued

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.