Create an ordered linked list Algorithm

Source: Internet
Author: User

# Include "stdafx. H"
# Include "stdlib. H"

Typedef struct Node
{
Int data;
Node * next;
};

Void insert (node * s, node *)
{
Node * P, * q;
P = NULL;
Q = NULL;
P = s;
If (p-> next = NULL) // empty linked list
{
P-> next = A; // A-> next is null.
S-> DATA = s-> Data + 1;
Return;
}
Q = P;
P = p-> next; // The linked list is not empty and points to the first node.
While (P! = NULL) & (p-> data <A-> data) // locate the appropriate insert Node
{
Q = P;
P = p-> next;
 
}

If (P = NULL) // you need to insert the end Of the link.
{
Q-> next = A; // A-> next is null.
S-> DATA = s-> Data + 1;
Return;
 
}
If (p-> DATA> = A-> data) // you need to insert the link intermediate node.
{
A-> next = P;
Q-> next =;
S-> DATA = s-> Data + 1;
Return;
}

}

Void main (INT argc, char * argv [])
{
Node * head = NULL;
Node * temp;
Int N;
Head = (node *) malloc (sizeof (node ));
Head-> DATA = 0; // chain table length, initially 0
Head-> next = NULL; // The linked list is empty.
Int I;
For (I = 0; I <5; I ++)
{

Printf ("input a integer/N ");
Scanf ("% d", & N );
Node * A = (node *) malloc (sizeof (node ));
A-> DATA = N;
A-> next = NULL;
Insert (Head, );

}

Printf ("the total node is % d/N", head-> data );
Temp = head-> next;
While (temp)
{
Printf ("% d/N", temp-> data );

Temp = temp-> next;
 
}

 

 

}

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.