# C program for Sorting Linked List

In previous articles we have seen how to create a linked list, delete elements from a linked list, types of linked list and lot more before going through this tutorial we recommend you to first read our previous articles on linked list, links given below

C Program to Delete a Node from Linked List

C program to perform implementation of Linked List

In this article, we will see how to sort the elements of a linked list in a particular order, for sorting we are going to use the bubble sorting technique. See the below C function to insert elements into a linked list

``void insert(int x){ struct node *temp,*j; if(head==NULL){ temp = (struct node *)malloc(sizeof(struct node)); head = temp; temp->val = x; temp->add = NULL; } else{ j = temp; temp = (struct node *)malloc(sizeof(struct node)); temp->val = x; temp->add = NULL; j->add = temp; } }``

The above `insert()` function will insert a new value into a linked list, we have already discussed `insert()` function in our previous articles , if you do not understand above code please do refer our previous articles links are given above.

Now we will make a display function to display the linked list data

``void display(){ struct node *j; j = head; while(j!=NULL){ printf("\%d ",j->val); j = j->add; } }``

Now we will see the sort() function which we will use to sort our linked list, see the below code snippet

``void sort(){ int temp; struct node *j,*i; for(j=head;j!=NULL;j=j->add){ for(i=j;i!=NULL;i=i->add){ if(i->val < j->val){ temp = i->val; i->val = j->val; j->val = temp;} } } }``

In the above sort function, we will traverse the whole linked list, the outer loop is used to fix pointer to an element and the inner loop will compare it with the all other values present in the linked list and compare them if the condition is satisfied the swapping will take place.

Complete program for Linked List sorting

``#include<stdio.h> #include<stdlib.h> struct node{ int val; struct node *add; }; struct node *head; void insert(int x); void del(int x); void display(); struct node *search(int x); void count(); void sort(); int main(){ insert(30); insert(29); insert(60); sort(); display(); count(); search(60); return 0; } void insert(int x){ struct node *temp,*j; if(head==NULL){ temp = (struct node *)malloc(sizeof(struct node)); head = temp; temp->val = x; temp->add = NULL; } else{ j = temp; temp = (struct node *)malloc(sizeof(struct node)); temp->val = x; temp->add = NULL; j->add = temp; } } void display(){ struct node *j; j = head; while(j!=NULL){ printf("\%d ",j->val); j = j->add; } } void count(){ struct node *j; j = head; int x=0; while(j!=NULL){ j = j->add; x++; } printf("Total number of node : \%d",x); } struct node *search(int x){ struct node *j; j = head; while(j!=NULL){ if(x==j->val){ return (j); } j = j->add; } return NULL; } void del(int x){ struct node *j,*k; j = (struct node *)search(x); if(j==head){ k = j->add; j->add = NULL; head = k; } else{ k = head; while(k->add!=j){ k = k->add; } k->add = j->add; j->add = NULL; } } void sort(){ int temp; struct node *j,*i; for(j=head;j!=NULL;j=j->add){ for(i=j;i!=NULL;i=i->add){ if(i->val < j->val){ temp = i->val; i->val = j->val; j->val = temp;} } } }``

Output Also, don’t forget to subscribe to our Newsletter.

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Macros in C with examples C Tutorials
30 January 2018

Armstrong Number program in C and C++
29 January 2018