Sort by linked list

Source: Internet
Author: User

Sort by linked list

A linked list is a non-consecutive, non-sequential Storage Structure in physical storage. The logical relationship between data is achieved through the pointer link order. The linked list is composed of a series of nodes, nodes can be dynamically generated at runtime. Each node consists of two parts: the data domain and the pointer domain storing the next node. A linked list is a common data structure.

To sort linked lists, you must first create a single-chain table. The program code is converted from an array. The Code is as follows:

First, create a structure of the node:

struct node{int val;node *next;};
Node * _ initial_node () // generates an empty linked list {node * head = new node; head-> val = 0; head-> next = NULL; return head ;} node * _ create (int * A, int length) // convert A linear array to A single-chain table {node * p1, * p2, * head; head = new node; head-> val = A [0]; p1 = new node; head-> next = p1; for (int I = 1; I <length; I ++) {if (I = length-1) p2 = NULL; elsep2 = new node; p1-> val = A [I]; p1-> next = p2; p1 = p2;} return head ;}
Sort by linked list can be applied to insert and Bubble sorting methods: This article uses insert sorting for Demonstration:

Design Philosophy: insert the values of arrays in the list to be sorted into a new empty list one by one to sort and insert the values in the array and list, if it is larger than a value in the linked list, insert the value to the front of the node. If all the numbers in the linked list are larger than the value, insert the value to the end of the linked list. The code for inserting the value is as follows:

void _add_node(node *A,int x){node *temp=A,*Base,*temp1;Base=new node;Base->val=x;if(temp->val<x)Base->next=temp;else{temp=A->next;temp1=A;while(temp){if(temp->val<x){Base->next=temp;temp1->next=Base;break;}temp1=temp;temp=temp->next;}if(temp==NULL){temp1->next=Base;Base->next=NULL;}}}
The following is the code for sorting the inserted chain table:

void main(){int ia[]={9,2,0,3,1,8,3};node *he=_create(ia,7);node *A=_initial_node();int num=he->val;A->val=num;for(he=he->next;he!=NULL;he=he->next){num=he->val;_add_node(A,num);}_show(A);}
The _ show () function is a function used to display the content of a linked list. The Code is as follows:

void _show(node *A){for(;A!=NULL;A=A->next){cout<<A->val<<" ";}cout<<endl;}
The test results are as follows:







C linked list sorting

Here is a piece of code that covers the Bubble sorting of the linked list!
# Include <stdio. h>
# Include <malloc. h>
Typedef struct node
{
Int data;/* data indicates the score */
Struct node * next;
} LNode, * LinkList;
LinkList Creat (void)/* creates a linked list. The End mark is when the input data is 0! */
{
LinkList H, p1, p2;
Int n;
N = 0;
P1 = p2 = (LinkList) malloc (sizeof (LNode ));
Printf ("input data :");
Scanf ("% d", & p1-> data );
H = NULL;
While (p1-> data! = 0)
{
N = n + 1;
If (n = 1)
H = p1;
Else
P2-> next = p1;
P2 = p1;
P1 = (LinkList) malloc (sizeof (LNode ));
Scanf ("% d", & p1-> data );
}
P2-> next = NULL;
Return (H );
}
LinkList Sort (LinkList SL)/* incremental sorting function: Entry parameter: Head pointer of the linked list. This is the sorting function in the linked list */
{
LinkList p, q;
Int temp;
For (p = SL; p! = NULL; p = p-> next)
{
For (q = p-> next; q! = NULL; q = q-> next)
{
If (p-> data> q-> data)
{
Temp = q-> data;
Q-> data = p-> data;
P-> data = temp;
}
}
}
Return SL;
}

Int main ()
{
LinkList L, S, K;
L = Creat ();
Printf ("the data sequence of the initialized single-chain table is \ n ");
For (S = L; S! = NULL; S = S-> next)
Printf ("% d", S-> data );
Sort (L );
Printf ("\ n: \ n ");
For (K = L; K! = NULL; K = K-> next)
Printf ("% d =>", K-> data );

Return 0;
}
I hope to help you. If you have any questions, please contact me again !... Remaining full text>

Single-chain table Bubble sorting program

# Include <stdio. h>
# Include <malloc. h>
Typedef struct node
{
Int data;/* data indicates the score */
Struct node * next;
} LNode, * LinkList;
LinkList Creat (void)/* creates a linked list. The End mark is when the input data is 0! */
{
LinkList H, p1, p2;
Int n;
N = 0;
P1 = p2 = (LinkList) malloc (sizeof (LNode ));
Printf ("input data (input 0 ends): \ n ");
Scanf ("% d", & p1-> data );
H = NULL;
While (p1-> data! = 0)
{
N = n + 1;
If (n = 1)
H = p1;
Else
P2-> next = p1;
P2 = p1;
P1 = (LinkList) malloc (sizeof (LNode ));
Scanf ("% d", & p1-> data );
}
P2-> next = NULL;
Return (H );
}
LinkList Sort (LinkList SL)/* incremental sorting function: Entry parameter: Head pointer of the linked list. This is the sorting function in the linked list */
{
LinkList p, q;
Int temp;
For (p = SL; p! = NULL; p = p-> next)
{
For (q = p-> next; q! = NULL; q = q-> next)
{
If (p-> data> q-> data)
{
Temp = q-> data;
Q-> data = p-> data;
P-> data = temp;
}
}
}
Return SL;
}

Int main ()
{
LinkList L, S, K;
L = Creat ();
Printf ("the data sequence of the initialized single-chain table is \ n ");
For (S = L; S! = NULL; S = S-> next)
Printf ("% d", S-> data );
Sort (L );
Printf ("\ n: \ n ");
For (K = L; K! = NULL; K = K-> next)
Printf ("% d =>", K-> data );

Return 0;
}

Related Article

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.