Pages

Tuesday, 27 August 2013

Concatenation of Linked List Alternatively

Hi, This program is about implementation of Concatenation of linked list alternatively

If you have any doubt regarding this program or any concept of C then please send me at khimanichirag@gmail.com or post a comment.

suggestion always appreciable

         #include<stdio.h>   
         #include<conio.h>   
         void concate_alternative();   
         typedef struct node   
         {   
           int data;   
           struct node *link;   
         }node;  
         node *first1=NULL,*last1,*first2=NULL,*last2;   
         void main()   
         {   
           int i=0;   
           node *temp;   
           printf("Enter the data in first link list");   
           do   
           {   
                  clrscr();   
                   temp=(node*)malloc(sizeof(node));   
                  i++;   
                  if(first1==NULL)   
                  {   
                     first1=temp;   
                     last1=temp;   
                     last1->link=NULL;   
                  }   
                  else   
                  {   
                     last1->link=temp;   
                     last1=temp;   
                     last1->link=NULL;   
                  }   
                   printf("\nEnter the %d element for first linked list: ",i);   
                   scanf("%d",&(last1->data));   
                   printf("\nYou want to enter more data(Y/N)");   
           }while(getch()=='y');  
           printf("\n\n\nYour first link list is:");   
           temp=first1;   
           while(temp!=NULL)   
           {   
                   printf("%d ",temp->data);   
              temp=temp->link;   
           }   
           getch();   
      //===========Scan data for second linked list=============   
           i=0;   
           clrscr();   
           printf("\n\nEnter the data in second link list");   
           do   
           {   
                   clrscr();   
                   temp=(node*)malloc(sizeof(node));   
                  i++;   
                   if(first2==NULL)   
                   {   
                    first2=temp;   
                    last2=temp;   
                    last2->link=NULL;   
                   }   
                   else   
                   {   
                    last2->link=temp;   
                    last2=temp;   
                    last2->link=NULL;   
                   }   
                  printf("\nEnter the %d element for second linked list: ",i);   
                  scanf("%d",&(last2->data));   
                  printf("\nYou want to enter more data(Y/N)");   
            }while(getch()=='y');   
            printf("\n\n\nYour second link list is:");   
            temp=first2;   
            while(temp!=NULL)   
            {   
                  printf("%d ",temp->data);   
                  temp=temp->link;   
            }   
            concate_alternative();   
            getch();   
      }   
         void concate_alternative()   
         {   
           node *temp1,*temp2,*first3=NULL,*last3,*temp;   
            int i=0;   
            temp1=first1;   
            temp2=first2;   
            while(temp1!=NULL && temp2!=NULL)   
            {   
                   if(i%2==0)   
                   {   
                      if(first3==NULL)   
                      {   
                           first3=temp1;   
                           last3=temp1;   
                      }   
                      else   
                      {   
                           last3->link=temp1;   
                           last3=temp1;   
                      }   
                      temp1=temp1->link;   
                      i++;   
                   }   
                   else   
                   {   
                      if(first3==NULL)   
                      {   
                           first3=temp2;   
                           last3=temp2;   
                      }   
                      else   
                      {   
                         last3->link=temp2;   
                         last3=temp2;   
                      }   
                      temp2=temp2->link;   
                    i++;   
                   }   
       }   
       if(temp1==NULL)   
       {   
             last3->link=temp2;   
       }   
       else if(temp2==NULL)   
       {   
             last3->link=temp1;   
       }   
       else   
       {   
             printf("\n\nCode is not working properly");   
       }   
       printf("\n\nYour final list is:");   
       temp=first3;   
       while(temp!=NULL)   
       {   
             printf("%d ",temp->data);   
         temp=temp->link;   
       }    
    }   

No comments:

Post a Comment