Advertisement
Arnab_Manna

CircularSIngleInsertSortedList

Oct 19th, 2022
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node_tag
  5. {
  6.     int Data;
  7.     struct node_tag *link;
  8. }node;
  9.  
  10. node *GetNode()
  11. {
  12.     node *New;
  13.     New=(node*)malloc(sizeof(node));
  14.     New->link=NULL;
  15.     return New;    
  16. }
  17.  
  18. void Display(node *Last)
  19. {
  20.     node *x;
  21.     x=Last->link;
  22.     if(x==NULL){
  23.         printf("empty");
  24.     }
  25.     while(x!=Last)
  26.     {
  27.         printf("%d,",x->Data);
  28.         x=x->link;
  29.     }
  30.     printf("%d,",Last->Data);
  31. }
  32.  
  33. /*the below function checks 3 conditions except NULL
  34. >>that are if the data is more than last it will insert at last
  35. >>if the data is smaller than 1st node then insert at begin
  36. >> else find a value that is equal to or greater than a
  37.    and deploy a pointer that is just one node ahead
  38.    now place the new link in between the two pointers*/
  39. node *Sortedlist(node *last,int a)
  40. {
  41.     node *y,*x,*New;
  42.     New=GetNode();
  43.     New->Data=a;
  44.     x=last;
  45.     if(last==NULL)
  46.     {
  47.         last=New;
  48.         last->link=last;
  49.     }
  50.     else if(last->Data<a)
  51.     {
  52.         New->link=last->link;
  53.         last->link=New;
  54.         last=New;
  55.     }
  56.     else if((last->link)->Data >= a)
  57.     {
  58.         New->link=last->link;
  59.         last->link=New;
  60.     }
  61.     else
  62.     {
  63.         x=last->link;
  64.             while(1)
  65.             {
  66.                 y=x;
  67.                 x=x->link;
  68.                 if(x->Data>=a)
  69.                 {
  70.                     break;
  71.                 }
  72.             }
  73.  
  74.             y->link=New;
  75.             New->link=x;
  76.         }
  77.     return last;
  78. }
  79.  
  80. node *CreateList()
  81. {
  82.     int ch,x,s,p1;
  83.     node *last=NULL;
  84.     while(1)
  85.     {
  86.         printf("enter 1 to continue and 0 to exit:");
  87.         scanf("%d",&ch);
  88.         if(ch==0){
  89.         break;}
  90.         else
  91.         {
  92.             printf("enter Data:");
  93.             scanf("%d",&x);
  94.             last=Sortedlist(last,x);
  95.             Display(last);
  96.             printf("\n");
  97.         }
  98.     }
  99.     return last;
  100. }
  101. int main()
  102. {
  103.     node *last;
  104.     last=CreateList();
  105.     Display(last);
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement