rajamit872

Implementation of Merging of Two Linked List(Ascending orde)

Oct 30th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 KB | None | 0 0
  1. #include<conio.h>
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4.  
  5. void add(struct node **,int);
  6. void display(struct node*);
  7. int count(struct node*);
  8. void merge(struct node*, struct node*,struct node**);
  9.  
  10. struct node
  11. {
  12. struct node *link;
  13. int data;
  14. };
  15.  
  16.  
  17. void main()
  18. {
  19.  
  20. struct node *first, *second, *third;
  21. first = second = third = NULL;
  22. clrscr();
  23. add(&first,4);
  24. add(&first,5);
  25. add(&first,567);
  26. add(&first,115);
  27. add(&first,547);
  28. add(&first,567);
  29.  
  30.  
  31. printf("\nFirst Linked List is ::\n");
  32. display(first);
  33. printf("No of element in the 1st linked list are::%d",count(first));
  34.  
  35. add(&second,51);
  36. add(&second,25);
  37. add(&second,-35);
  38. add(&second,45);
  39.  
  40. printf("\nSecond Linked List is ::\n");
  41. display(second);
  42. printf("No of element in the 2nd linked list are::%d",count(second));
  43.  
  44. merge(first,second,&third);
  45.  
  46. printf("\nMerged Linked List is ::\n");
  47. display(third);
  48. printf("No of element in the linked list are::%d",count(third));
  49.  
  50.  
  51. getch();
  52. }
  53.     void add(struct node**q, int num)
  54.     {
  55.       struct node *temp=*q,*r;
  56.       r = (struct node*) malloc(sizeof(struct node));
  57.       r->data = num;
  58.  
  59.       /*if the list is empty or the element is added to the begning*/
  60.       if(*q==NULL || (*q)->data>num)
  61.       {
  62.         *q=r;
  63.         (*q)->link=temp;
  64.       }
  65.       else
  66.       {
  67.         /*traverse the entire linked list to
  68.         search the appropriated position*/
  69.         while(temp!=NULL)
  70.         {
  71.         if(temp->data < num && (temp->link==NULL || temp->link->data>num))
  72.         {
  73.            r->link=temp->link;
  74.            temp->link=r;
  75.            return;
  76.         }
  77.         temp=temp->link;
  78.         }
  79.         r->link=NULL;
  80.         temp->link=r;
  81.  
  82.       }
  83.     }
  84.  
  85.     /*Displaying the content in the linked list*/
  86.     void display(struct node *q)
  87.     {
  88.       /*Traverse the entire linked list*/
  89.       while(q!=NULL)
  90.       {
  91.         printf("%d  ",q->data);
  92.         q=q->link;
  93.       }
  94.       printf("\n");
  95.  
  96.     }
  97.  
  98.  
  99.     /*Count the linked list's element*/
  100.     int count(struct node *q)
  101.     {
  102.       int c=0;
  103.       while(q!=NULL)
  104.       {
  105.       q=q->link;
  106.       c++;
  107.       }
  108.       return c;
  109.     }
  110.  
  111.  
  112.     /*Merging the both linked list and store in the 3rd linked list*/
  113.     void merge(struct node *p, struct node *q, struct node **s)
  114.     {
  115.       struct node *z;
  116.       z=NULL;
  117.  
  118.       /*if both the linked list is empty*/
  119.       if(p==NULL && q==NULL)
  120.       return;
  121.  
  122.       /*Traverse the both linked list till the end of atleast one linked list*/
  123.       while(p!=NULL && q!=NULL)
  124.       {
  125.         /*if node is being added at the starting*/
  126.         if(*s==NULL)
  127.         {
  128.           *s= (struct node *) malloc (sizeof(struct node));
  129.           z=*s;
  130.         }
  131.         else
  132.         {
  133.           z->link= (struct node *)malloc (sizeof(struct node));
  134.           z=z->link;
  135.         }
  136.  
  137.         if(p->data < q->data)
  138.         {
  139.           z->data=p->data;
  140.           p=p->link;
  141.         }
  142.         else
  143.         {
  144.           if(p->data > q->data)
  145.           {
  146.         z->data=q->data;
  147.         q=q->link;
  148.           }
  149.           else
  150.           {
  151.         if(p->data == q->data)
  152.         {
  153.           z->data = q->data;
  154.           p=p->link;
  155.           q=q->link;
  156.         }
  157.           }
  158.         }
  159.       }
  160.  
  161.  
  162.  
  163.     /*if the end of the first node has not reached*/
  164.     while(p!=NULL)
  165.     {
  166.      z->link=(struct node*) malloc (sizeof(struct node));
  167.      z=z->link;
  168.      z->data = p->data;
  169.      p=p->link;
  170.     }
  171.  
  172.     /*if the end of the second node has not reached*/
  173.     while(q!=NULL)
  174.     {
  175.      z->link=(struct node*) malloc (sizeof(struct node));
  176.      z=z->link;
  177.      z->data = q->data;
  178.      q=q->link;
  179.     }
  180.     z->link=NULL;
  181.     }
Advertisement
Add Comment
Please, Sign In to add comment