Advertisement
Arnab_Manna

CircularSIngleMerge2list

Oct 18th, 2022
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 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. node *InsertAtEnd(node *Last,int x)
  19. {
  20.     node *New;
  21.     New=GetNode();
  22.     New->Data=x;
  23.     if(Last==NULL)
  24.     {
  25.         Last=New;
  26.     }
  27.         New->link=Last->link;
  28.         Last->link=New;
  29.         Last=New;
  30.     return Last;
  31. }
  32. node *Merge(node *Last1,node *Last2)
  33. {
  34.     node *x;
  35.     x=Last2->link;
  36.     Last2->link=Last1->link;
  37.     Last1->link=x;
  38.  
  39.     return Last2;
  40. }
  41. node *CreateList()
  42. {
  43.     int ch,x;
  44.     node *Last=NULL;
  45.     while(1)
  46.     {
  47.         printf("enter 0 to stop :");
  48.         scanf("%d",&ch);
  49.         if(ch==0)return Last;
  50.         else
  51.         {  
  52.             printf("enter the data :");
  53.             scanf("%d",&x);
  54.             Last=InsertAtEnd(Last,x);
  55.         //  Display(Last);
  56.         }
  57.     }
  58. }
  59. void Display(node *Last)
  60. {
  61.     node *x;
  62.     x=Last->link;
  63.     while(x!=Last)
  64.     {
  65.         printf("%d,",x->Data);
  66.         x=x->link;
  67.     }
  68.     printf("%d,",Last->Data);
  69. }
  70. int main()
  71. {
  72.     node *Last1,*Last2,*Last3;
  73.     printf("LIST 1:\n");
  74.     Last1=CreateList();
  75.     printf("\nLIST 1:");
  76.     Display(Last1);
  77.    
  78.     printf("LIST 2:\n");
  79.     Last2=CreateList();
  80.     printf("\nLIST 2:");
  81.     Display(Last2);
  82.    
  83.     printf("LIST 3 after Concatinaion:\n");
  84.     Last3=Merge(Last1,Last2);
  85.     printf("\nLIST 3:");
  86.     Display(Last3);
  87.    
  88.     return 0;  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement