Write a Program in C to sort a linked list containing numbers using any sorting technique of your choice.

DATA STRUCTURE DEFINITION

_______________________________________________

NODE: Data {To store data  in  the Node}

                                                      Next {To store link of  the next Node}

                ______________________________________________________________


Sorting of singly linked list Algorithm: https://browncodeit.blogspot.com/2020/06/an-algorithm-for-singly-linked-list-sorting.html

Coding in C

Source Code:

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4.  
  5. //Definition of the data structure
  6. struct node
  7. {
  8.   //parts of node
  9.  int data;
  10.  struct node *next;
  11. };
  12.  
  13. //Rename of the data structure
  14. typedef struct node node;
  15.  
  16. //Function prototype declarations
  17. node *createlist (node *head);
  18. node *sort(node *head);
  19. void display(node *head);
  20. int main()
  21. {
  22.    //Declaration of variable
  23.   int c;
  24.     node *head;
  25.     //Allocation memory dynamically for the variable
  26.     head = (node *)malloc(sizeof(node));
  27.     do
  28.      {
  29.         //Displaying and calling the functions
  30.         printf("\nCreation of a Linked List");
  31.         head = createlist(head);
  32.         printf("\nContents of Linked List :\n");
  33.         display(head); 
  34.         printf("\nSorting of Linked List :\n");
  35.        head = sort(head);
  36.         printf("\n Do you want to continue ? press 1 : ");
  37.      scanf("%d",&c);
  38.       }while(c==1);
  39.         getch();
  40. }
  41.  
  42. //Function to create a linked list
  43. node *createlist (node *head)
  44. {
  45.    //Declaration of variables
  46. int n, i;
  47.    node *list;
  48.    //Input total number
  49.    printf("\nEnter total number of elements in the list : ");
  50.    scanf("%d",&n);
  51.    //Initioalizing the list by head pointer
  52.    list = head;
  53.    for( i = 0; i<n; i++)
  54.      {
  55. printf("Enter data :");
  56.         scanf("%d", &list->data);
  57.         if( i == n-1)
  58.               list->next = NULL;
  59.           else 
  60.             {                                 
  61.               list->next=(node *)malloc(sizeof(node));
  62.               list = list->next;               
  63.              }
  64.        }
  65.        return head;
  66. }
  67.  
  68. //Function to display a linked list
  69. void display (node *head)
  70. {
  71.    //Declaration of variables
  72.     node *list;
  73.    //Initializing the list by pointer head
  74.    list= head;
  75.    while(list->next != NULL)
  76.     {
  77.                printf("%d ->",list->data);
  78.                list = list -> next;
  79.     }
  80.      printf("%d\n",list->data);
  81. }

  82. //Funtion to sort a linked list
  83. node *sort(node *head)
  84. {
  85.    //Declaration of Variables
  86.    int temp;
  87.    node *x,*y;
  88.    for(x=head;x!=NULL;x=x->next)
  89.     {
  90.       for(y=x->next;y!=NULL;y=y->next)
  91.         {
  92.            if(x->data>y->data)
  93.              {
  94.               temp = x->data;
  95.               x->data = y->data;
  96.               y->data = temp;
  97.                }
  98.         }
  99.     }

  100.  //Print the linked list in sorted order
  101.  printf("\nContents of Linked List in sorted order : \n");
  102.  display(head);
  103.  return head;
  104. }


Output

 Outputs:-

Creation of a Linked List

Enter total number of elements in the list : 3

Enter data :15

Enter data :12

Enter data :18

 

Contents of Linked List :

15 -> 12 -> 18

 

Sorting of Linked List :

 

Contents of Linked List in sorted order :

12 -> 15 -> 18

 

 Do you want to continue ? press 1 : 1

 

Creation of a Linked List

Enter total number of elements in the list : 4

Enter data :66

Enter data :55

Enter data :

44

Enter data :77

 

Contents of Linked List :

66 -> 55 -> 44 -> 77

 

Sorting of Linked List :

 

Contents of Linked List in sorted order :

44 -> 55 -> 66 -> 77


0 comments:

Post a Comment