Advertisement
Aniket_Goku

sll_selection_Sort

Dec 24th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. //selection list linked list
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<stdlib.h>
  5. int val;
  6. struct sll
  7. {
  8.     int info;
  9.     struct sll *next;
  10. }*prev=NULL,*temp=NULL,*temp2=NULL,*start=NULL,*node=NULL;
  11. void create_node()
  12. {
  13.     node=(struct sll*)malloc(sizeof(struct sll));
  14.     if(node==NULL)
  15.     {
  16.         printf("\n Node is not allocated in memory");
  17.         return;
  18.     }
  19.     node->next=NULL;
  20.    
  21. }
  22. void disp()
  23. {
  24.    
  25.     if(start==NULL)
  26.     {
  27.         printf("\n Sll is empty to display");
  28.         return;
  29.     }
  30.     printf("\n Display SLL\n");
  31.     temp=start;
  32.     while(temp!=NULL)
  33.     {
  34.         printf("\n %d",temp->info);
  35.         temp=temp->next;
  36.     }
  37. }
  38. void in_beg()
  39. {
  40.     create_node();
  41.     printf("\n Enter the value: ");
  42.     scanf("%d",&val);
  43.     node->info=val;
  44.    
  45.     if(start==NULL)
  46.     {
  47.         start=node;
  48.         return;
  49.     }
  50.     node->next=start;
  51.     start=node;
  52.    
  53.    
  54. }
  55. void in_end()
  56. {
  57.     if(start==NULL)
  58.     {
  59.         in_beg();
  60.         return;
  61.     }
  62.     temp=start;
  63.     while(temp->next!=NULL)
  64.     {
  65.        
  66.         temp=temp->next;
  67.     }
  68.     create_node();
  69.     printf("\n Enter the value: ");
  70.     scanf("%d",&val);
  71.     temp->next=node;
  72.     node->info=val;
  73.    
  74.    
  75.    
  76. }
  77. void del_beg()
  78. {
  79.     if(start==NULL)
  80.     {
  81.         printf("\n Sll Is empty for delete");
  82.         return;
  83.     }
  84.     if(start->next==NULL)
  85.     {
  86.         printf("\n %d is deleted",start->info);
  87.         free(start);
  88.         start=NULL;
  89.         return;
  90.     }
  91.     temp=start->next;
  92.     printf("\n %d is deleted",start->info);
  93.     free(start);
  94.    
  95.     start=temp;
  96.  
  97. }
  98. void del_end()
  99. {
  100.     if(start==NULL)
  101.     {
  102.         printf("\n SLl is empty for delete");
  103.         return;
  104.     }
  105.     if(start->next==NULL)
  106.     {
  107.         del_beg();
  108.         return;
  109.     }
  110.     temp=start;
  111.     while(temp->next!=NULL)
  112.     {
  113.         prev=temp;
  114.         temp=temp->next;
  115.     }
  116.     prev->next=NULL;
  117.     printf("\n %d is Deleted",temp->info);
  118.     free(temp);
  119.     disp();
  120.    
  121. }
  122. void selected_sort()
  123. {
  124.     int i,j;
  125.     if(start==NULL )
  126.     {
  127.         printf("\n Sll is empty for sorting");
  128.         return;
  129.     }  
  130.     if(start->next==NULL)
  131.     {
  132.         printf("\n display sorted  dll");
  133.         printf("\n %d",start->info);
  134.         return;
  135.     }
  136.     temp=start;
  137.     while(temp!=NULL)
  138.     {
  139.         temp2=temp;
  140.         while(temp2!=NULL)
  141.         {
  142.            
  143.             if((temp->info)>(temp2->info))
  144.             {
  145.                
  146.                 val=temp2->info;
  147.                
  148.                 temp2->info=temp->info;
  149.                
  150.                 temp->info=val;
  151.                
  152.             }
  153.             temp2=temp2->next;
  154.         }
  155.         temp=temp->next;
  156.     }
  157.     disp();
  158. }
  159. void main()
  160. {
  161.     int ch;
  162.     do
  163.     {
  164.         printf("\n \t\t\t ===>  Menu  <===");
  165.         printf("\n 1.insert from begining");
  166.         printf("\n 2.insert from end");
  167.         printf("\n 3.delete from begining");
  168.         printf("\n 4.delete from end");
  169.         printf("\n 5.Sort ascending order");
  170.         printf("\n 6. Display sll");
  171.         printf("\n Enter your choice: ");
  172.         scanf("%d",&ch);
  173.         switch(ch)
  174.         {
  175.             case 1:
  176.                 in_beg();
  177.                 disp();
  178.                 break;
  179.             case 2:
  180.                 in_end();
  181.                 disp();
  182.                 break;
  183.             case 3:
  184.                 del_beg();
  185.                 disp();
  186.                 break;
  187.             case 4:
  188.                 del_end();
  189.                 break;
  190.             case 5:
  191.                 selected_sort();
  192.                 break;
  193.             case 6:
  194.                 disp();
  195.                 break;
  196.         }
  197.     }while(ch<=6 && ch>=1);
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement