Delete, insert, and sort a single-chain table.

Source: Internet
Author: User

# Define false 0
# Define true 1

Typedef struct node {
Struct node * link;
Int value;
} Node;

Int length (node * head)
{
Node * current;
Int length = 0;
Current = head;

While (current! = NULL)
{
Current = Current-> link;
Length ++;
}
Return length;
}

Node * Create ()
{
Node * head, * P, * s;
Int value, cycle = 1;

Head = (node *) malloc (sizeof (node ));
P = head;
While (cycle)
{
Printf ("/nplease input the data :");
Scanf ("% d", & value );
If (value! = 0)
{
S = (node *) malloc (sizeof (node ));
S-> value = value;
Printf ("/n s-> value = % d", S-> value );
P-> link = s;
P = s;
}
Else
Cycle = 0;
}
Head = head-> link;
P-> link = NULL;
Return head;
}

Void print (node * head)
{
Node * P = NULL;
Int n = 0;
N = length (head );
Printf ("/n now, these % d records are:/N", N );
P = head;
While (P! = NULL)
{
Printf ("/n p-> Value % d", p-> value );
P = p-> link;
}
Printf ("/N ");
}

Node * reserve (node * head)
{
Node * P1, * P2, * P3;
Printf ("/nentry reserve () ===== begin !!!! ");
If (Head = NULL | head-> link = NULL)
Return head;
P1 = head;
P2 = p1-> link;
While (P2)
{
P3 = P2-> link;
P2-> link = p1;
P1 = P2;
P2 = P3;
}
Head-> link = NULL;
Head = p1;
Printf ("/nentry reserve () ===== end !!!! ");
Return head;
}

Int sll_insert (node ** rootp, int new_value)
{
Register node * current;
Register node * _ new;
Printf ("/nentry sll_insert () ====== begin !!!! ");
While (current = * rootp )! = NULL & Current-> value <new_value)
{
Rootp = & Current-> link;
}

_ New = (node *) malloc (sizeof (node ));
If (_ new = NULL)
Return false;
_ New-> value = new_value;

_ New-> link = current;
* Rootp = _ new;

Printf ("/nentry sll_insert () ===== end !!!! ");
Return true;
}

Node * del (node * head, int value)
{
Node * P1, * P2;
P1 = head;
Printf ("/nentry del () ====== begin !!!! ");
While (P1-> link! = NULL & value! = P1-> value)
{
P2 = p1;
P1 = p1-> link;
}

If (value = p1-> value)
{
If (p1 = head)
{
Head = p1-> link;
Free (P1 );
}
Else
{
P2-> link = p1-> link;
Free (P1 );
}
}
Else
{
Printf ("/n % d is cocould not been found", value );
Return NULL;
}
Printf ("/nentry del () ====== end !!!! ");
Return head;
}

Node * sort (node * head)
{
Node * P;
Int temp;
Int N;
Int I, J;
Printf ("/nentry sort () ===== begin !!!! ");
N = length (head );

If (Head = NULL | head-> link = NULL)
Return head;

For (j = 1; j <n; j ++)
{
P = head;
For (I = 0; I <n-J; I ++)
{
If (p-> value> P-> link-> value)
{
Temp = p-> value;
P-> value = p-> link-> value;
P-> link-> value = temp;
}
P = p-> link;
}
}
Printf ("/nentry sort () ===== end !!!! ");
Return head;
}

Int main (void)
{
Node * head = NULL;
Node * _ new = NULL;
Node * returnp = NULL;
Node * delp = NULL;
Node * sortp = NULL;
Int length = 0;
Head = create ();
Print (head );
_ New = reserve (head );
Print (_ new );
Sll_insert (& _ new, 100 );
Print (_ new );
Delp = del (_ new, 4 );
Print (delp );
Sortp = sort (delp );
Print (sortp );
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.