Operations on a single-chain table using C language (2)

Source: Internet
Author: User

Previous Article <Operations on a single-chain table using C language (1)>It is mainly about some of the most basic operations of a single-chain table. below, it is mainly about some other typical algorithms and test programs. Copy codeThe Code is as follows:/* sort a single-chain table */
Struct LNode * sort (struct LNode * head)
{
LinkList * p;
Int n, I, j;
Int temp;
N = ListLength (head );
If (head = NULL | head-> next = NULL)
Return head;
P = head-> next;
For (j = 1; j <n; ++ j)
{
P = head-> next;
For (I = 0; I <n-j; ++ I)
{
If (p-> data> p-> next-> data)
{
Temp = p-> data;
P-> data = p-> next-> data;
P-> next-> data = temp;
}
P = p-> next;
}
}
Return head;
}
/* Reverse set a single-chain table */
LinkList * reverse (LinkList * head)
{
LinkList * p1, * p2 = NULL, * p3 = NULL;
If (head = NULL | head-> next = NULL)
Return head;
P1 = head-> next;
While (p1! = NULL)
{
P3 = p1-> next;
P1-> next = p2;
P2 = p1;
P1 = p3;
}
Head-> next = p2;
// Head = p2;
Return head;
}
Status equal (ElemType c1, ElemType c2)
{
If (c1 = c2)
Return TRUE;
Else
Return FALSE;
}
/* Insert all data elements in the linear Lb table but not in La into La */
Void Union (LinkList * La, LinkList * Lb)
{
ElemType * e;
Int La_len, Lb_len;
Int I;
La_len = ListLength (La );
Lb_len = ListLength (Lb );
For (I = 1; I <= Lb_len; I ++)
{
GetElem (Lb, I, e); // obtain the I-th element in Lb and assign it to e.
If (! LocateElem (La, * e, equal) // if the same element as e does not exist in La, insert
ListInsert (La, ++ La_len, * e );
}
}
Void print (ElemType c)
{
Printf ("% 4d", c );
}
/* Merge two single-chain tables. The data in La and Lb are arranged in non-descending order, and the merged Lc or non-descending order */
Void MergeList (LinkList * La, LinkList * Lb, LinkList ** Lc)
{
Int I = 1, j = 1, k = 0;
Int La_len, Lb_len;
ElemType * ai, * bj;
Ai = (ElemType *) malloc (sizeof (ElemType ));
Bj = (ElemType *) malloc (sizeof (ElemType ));

InitList (Lc );
La_len = ListLength (La );
Lb_len = ListLength (Lb );
While (I <= La_len & j <= Lb_len)
{
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 );
}
}
/* Traverse only once and find the intermediate node in the single-chain table
1 defines two pointers. one pointer moves two steps (fast pointer) at a time, and the other pointer moves one data (slow pointer) at a time)
2. When the fast pointer reaches the end of the linked list, the slow pointer goes to the intermediate node of the linked list.
In the program, you can also determine whether a single-chain table has loops. If the fast pointer can catch up with the slow pointer, otherwise it will end with NULL */
LinkList * Searchmid (LinkList * head)
{
If (NULL = head)
Return NULL;
If (head-> next = NULL)
Return head;
If (head-> next = NULL)
Return head;

LinkList * mid = head;
LinkList * p = mid-> next;

While (p! = NULL) & (NULL! = P-> next ))
{
Mid = mid-> next;
P = p-> next;
}
Return mid;
}

The following is a test procedure for a single-chain table.Copy codeThe Code is as follows: Status comp (ElemType c1, ElemType c2)
{
If (c1 = c2)
Return TRUE;
Else
Return FALSE;
}
Void visit (ElemType c)
{
Printf ("% 4d", c );
}
Void main ()
{
LinkList * L;
LinkList * mid;
Mid = (struct LNode *) malloc (sizeof (struct LNode ));

ElemType * e, e0, * e1;
Status I;
Int j, k;
E = (ElemType *) malloc (sizeof (ElemType ));
E1 = (ElemType *) malloc (sizeof (ElemType ));

I = InitList (& L );
For (j = 1; j <= 6; j ++)
{
I = ListInsert (L, 1, j );
}
Printf ("insert 1 to the L header in sequence ~ After 6: L = ");
ListTraverse (L, visit );
Printf ("L intermediate node value: mid = :");
Mid = Searchmid (L );
Printf ("% d \ n", mid-> data );

Printf ("L reverse output: L = ");
ListTraverse (reverse (L), visit );
Printf ("L: L = ");
ListTraverse (sort (L), visit );

I = ListEmpty (L );
Printf ("whether L is null: I = % d (1: Yes, 0: No) \ n", I );
I = ClearList (L );
Printf ("after clearing L: L = ");
ListTraverse (L, visit );
I = ListEmpty (L );
Printf ("whether L is null: I = % d \ n", I );
For (j = 1; j <= 10; j ++)
{
ListInsert (L, j, j );
}
Printf ("insert 1 ~ at the end of the L table in sequence ~ After 10: L = ");
ListTraverse (L, visit );
GetElem (L, 5, e );
Printf ("the value of 5th elements is % d \ n", * e );
For (j = 0; j <= 1; j ++)
{
K = LocateElem (L, j, comp );
If (k)
Printf ("the value of element % d is % d \ n", k, j );
Else
Printf ("element \ n with no value of % d", j );
}
For (j = 1; j <= 2; j ++)
{
GetElem (L, j, e1 );
I = PriorElem (L, * e1, e );
If (I = INFEASIBLE)
Printf ("element % d NO precursor \ n", * e1 );
Else
Printf ("the precursor of element % d is % d \ n", * e1, * e );
}
For (j = ListLength (L)-1; j <= ListLength (L); j ++)
{
GetElem (L, j, e1 );
I = NextElem (L, * e1, e );
If (I = INFEASIBLE)
Printf ("element % d no successor \ n", * e1 );
Else
Printf ("the successor of element % d is: % d \ n", * e1, * e );
}
K = ListLength (L );
For (j = k + 1; j> = k; j --)
{
I = ListDelete (L, j, e );
If (I = ERROR)
Printf ("failed to delete the % d Data \ n", j );
Else
Printf ("the deleted element is % d \ n", * e );
}
Printf ("elements that output L in sequence :");
ListTraverse (L, visit );
DestroyList (L );
Printf ("after L is destroyed: L = % u \ n", L );
Printf ("************************************* * *********** \ n ");
LinkList * La, * Lb;
I = InitList (& La );
If (I = 1)
For (j = 1; j <= 5; j ++)
I = ListInsert (La, j, j );
Printf ("La = ");
ListTraverse (La, print );
InitList (& Lb );
For (j = 1; j <= 5; j ++)
I = ListInsert (Lb, j, 2 * j );
Printf ("Lb = ");
ListTraverse (Lb, print );
Union (La, Lb );
Printf ("new La = ");
ListTraverse (La, print );
Printf ("************************************* * *********** \ n ");
LinkList * La_1, * Lb_1, * Lc_1;
Int a [4] = {,}, B [7] = {, 20 };
InitList (& La_1 );
For (j = 1; j <= 4; j ++)
ListInsert (La_1, j, a [J-1]);
Printf ("La_1 = ");
ListTraverse (La_1, print );
InitList (& Lb_1 );
For (j = 1; j <= 7; j ++)
ListInsert (Lb_1, j, B [J-1]);
Printf ("Lb_1 = ");
ListTraverse (Lb_1, print );
MergeList (La_1, Lb_1, & Lc_1 );
Printf ("Lc_1 = ");
ListTraverse (Lc_1, print );
}

The following are some running results in Linux:Copy codeThe Code is as follows: insert 1 to the L header in sequence ~ After 6: L = 6 5 4 3 2 1
L The value of the intermediate node is mid =: 4
L reverse output: L = 1 2 3 4 5 6
L: L = 1 2 3 4 5 6
L empty: I = 0 (1: Yes, 0: No)
After clearing L: L =
L empty: I = 1
Insert 1 to the end of the L table in sequence ~ After 10: L = 1 2 3 4 5 6 7 8 9 10
The value of the 5th elements is 5.
No element with a value of 0
The value of the 1st elements is 1.
Element 1 has no precursor
The precursor of element 2 is: 1
The successor of Element 9 is 10.
Element 10 has no successor
An error occurred while deleting the 11th data records.
The deleted element is 10.
Output L elements in sequence: 1 2 3 4 5 6 7 8 9
After L is destroyed: L = 7954544
**************************************** *********
La = 1 2 3 4 5
Lb = 2 4 6 8 10
New La = 1 2 3 4 5 6 8 10
**************************************** *********
La_1 = 3 5 8 11
Lb_1 = 2 6 8 9 11 15 20
Lc_1 = 2 3 5 6 8 9 11 11 15 20

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.