Data Structure algorithm-2

Source: Internet
Author: User

12. Combine Two ordered linked lists into an ordered linked list.

Void MergeList_L (LinkList & La, LinkList & Lb, LinkList & Lc)

{

// It is known that the elements of the single-chain linear table La and Lb are arranged in descending order of values, and the new single-chain linear table // Lc is obtained by combining La AND Lb, and the elements of Lc are also arranged in descending order of values.

Pa = La-> next;

Pb = Lb-> next;

Lc = pc = La; // use the La header node as the Lc header Node

While (pa & pb)

{

If (pa-> data <= pb-> data)

{

Pc-> next = pa; pc = pa; pa = pa-> next;

}

Else {pc-> next = pb; pc = pb; pb = pb-> next ;}

}

Pc-> next = pa? Pa: pb; // Insert the remaining segments

Free (Lb); // release the Lb header Node

}

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

13. The LocateElem positioning function in the static linked list is as follows:

Int LocateElem_SL (SLinkList S, ElemType e)

{

// Search for 1st elements with e values in the static single-link linear table L. If e is found, the Order in L is returned. // otherwise, 0 is returned.

I = S [0]. cur; // I indicates the first node in the table

While (I & S [I]. data! = E) I = S [I]. cur;

Return I;

}

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

14. The static linked list algorithm is discussed by using the set operation (A-B) U (B-A) as an example.

Void InitSpace_SL (SLinkList & space)

{

// Chain each component in the space of a one-dimensional array into a backup linked list. space [0]. cur is the header pointer, and 0 indicates a null pointer.

For (I = 0; I

Space [MAXSIZE-1]. cur = 0;

}

Void Free_SL (SLinkList & space, int k)

{

// Reclaim the idle node with the subscript k to the backup linked list

Space [k]. cur = space [0]. cur; space [0]. cur = k;

}

Void difference (SLinkList & space, int & S)

{

// Input the elements of the Set A and B in turn, and create A collection (A-B) U (B-A) in the one-dimensional array space

// Static linked list, with S as its header pointer. Assuming that the standby space is large enough, space [0]. cur is its header pointer.

InitSpace_SL (space); // initialize the backup space

S = Malloc_SL (space); // generate the head node of S

R = S; // r points to the current last node of S.

Scanf (m, n); // enter the number of elements A and B

For (j = 1; j <= m; ++ j

// Create A linked list of set

{

I = Malloc. SL (space); // distribution node

Scanf (space [I]. data); // enter the element value of

Space [r]. cur = I; r = I; // insert to the end of the table

}

Space [r]. cur = 0;

For (j = 1; j <= n; ++ j) // input the elements of B in sequence. If the elements are not in the current table, insert them. Otherwise, delete them.

{

Scanf (B); p = S; k = space [S]. cur; // k points to the first node in set.

While (k! = Space [r]. cur & space [k]. data! = B)

{// Search in the current table

P = k; k = space [k]. cur;

}

If (k = space [r]. cur)

{

// This element does not exist in the current table. It is inserted after the node indicated by r and the location of r remains unchanged.

I = Malloc_SL (space );

Space [I]. data = B;

Space [I]. cur = space [r]. cur;

Space [r]. cur = I;

}

Else {// This element is already in the Table. Delete it

Space [p]. cur = space [k]. cur;

Free_SL (space, k );

If (r = k) r = p; // if you delete a node referred to by r, You need to modify the tail pointer.

}

}

}

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

15. insert and delete data in a two-way linked list

StatusListInsert_DuL (DuLinkList & L, int I, ElemType e)

{

// Insert element e before position I in the double-chain cyclic linear table L at the leading node. The valid value of I is 1 <= I <= table length + 1

If (! (P = GetElemp_DuL (L, I) // determine the insert position in L

Return ERROR; // p = NULL, that is, the Insertion Location is invalid.

If (! (S = (DuLinkList) malloc (sizeof (DuLNode) return ERROR;

S-> data = e;

S-> prior = p-> prior; p-> prior-> next = s;

S-> next = p; p-> prior = s;

Return OK;

}

StatusListDelete_DuL (DuLinkList & L, int I, ElemType & e)

{

// Delete the I-th element of the double-chain cyclic linear table L at the leading node. The valid value of I is 1 <= I <= table length

If (! (P = GetElemp_DuL (L, I) // determines the position pointer of the I element in L.

Return ERROR; // p = NULL, that is, the I-th element does not exist.

E = p-> data;

P-> prior-> next = p-> next;

P-> next-> prior = p-> prior;

Free (p); return OK;

}

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.