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

Also read
Understanding the Concepts of Linked List-The Easy Way

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.

C program to sort Linked List

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

C program to sort linked list

Thank you for reading

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

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

If you like this article, then please share it and help us grow.


C program to generate all combinations of N-Bit Binary String
10 July 2018

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

Creating a Fixed Header Navigation Bar with on scroll effect
28 January 2018

C program to find Modular Multiplicative Inverse of two Relatively Prime Numbers
25 January 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий