Thursday, April 12, 2012

LinkList – Concatenation of Two List



#include <stdio.h>
#include <conio.h>
  //Concatenation of two List
  typedef struct node
      {
           int n;
           struct node *next;
      }List;
  List *ptr;
  void Insert(List **first)
      {
           List *temp,*cur;
           if(ptr!=NULL)
              {
                   ptr->next=NULL;
                   printf("\n\tClearing Concatenate Data from First List\n");
              }
           temp=(List *)malloc(sizeof(List));
           printf("\n\tEnter the Value : ");
           scanf("%d",&temp->n);
           if(*first==NULL)
              {
                   *first=cur=temp;
                   temp->next=NULL;
              }
           else
              {
                   for(cur=*first;cur->next!=NULL;cur=cur->next);
                   cur->next=temp;
                   temp->next=NULL;
              }
      }

  void disp(List *first)
      {
           List *cur;
           for(cur=first;cur!=NULL;cur=cur->next)
              {
                   printf("%d ",cur->n);
              }
           getch();
           printf("\n");
      }

      void concat(List *first,List *second)
           {
              List *cur;
              ptr=NULL;
              printf("\n\tSecond List Concatenate with First : \n");
              for(cur=first;cur->next!=NULL;cur=cur->next);
              ptr=cur;
              cur->next=second;
           }

  void main()
      {
           int c=0,ch=0;
           List *head,*head1;
           clrscr();
           head=head1=ptr=NULL;
           while(c!=4)
              {
                   clrscr();
                   fflush(stdin);
                   printf("\n\n\t1. Add");
                   printf("\n\t2. Display");
                   printf("\n\t3. Concat");
                   printf("\n\t4. Exit");
                   printf("\n\tEnter your choice : ");
                   scanf("%d",&c);
                   fflush(stdin);
                   switch(c)
                        {
                            case 1:
                                ch=0;
                                while(ch!=3)
                                    {
                                         printf("\n\t1. List-1");
                                         printf("\n\t2. List-2");
                                         printf("\n\t3. Exit");
                                         printf("\n\tEnter Choice : ");
                                         scanf("%d",&ch);
                                         switch(ch)
                                              {
                                                  case 1:       Insert(&head);break;
                                                  case 2:       Insert(&head1);break;
                                           }
                                         fflush(stdin);
                                    }
                                         break;
                            case 2:
                                ch=0;
                                while(ch!=3)
                                    {
                                         printf("\n\t1. List-1");
                                         printf("\n\t2. List-2");
                                         printf("\n\t3. Exit");
                                         printf("\n\tEnter Choice : ");
                                         scanf("%d",&ch);
                                         fflush(stdin);
                                         switch(ch)
                                         {
                                              case 1:  printf("\n\tFirst List : ");
                                                            disp(head);break;
                                              case 2:  printf("\n\tSecond List : ");
                                                            disp(head1);break;
                                         }
                                  }
                                         break;
                            case 3:   concat(head,head1);
                                         printf("\n\tFirst List, After Concat : ");
                                         disp(head);break;
                            case 4:   printf("\n\tQuit....");
                                         getch();
                                         exit(0);
                            default:printf("\nWrong no");
                        }
              }
           getch();
      }



No comments: