Wednesday 11 February 2015

WAP to concatenate two singly linked lists.



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
      int data;
      struct node *next;
}node;
main()
{
      node *head1,*head2,*head,*p;
      int n1,n2,i,j;
      printf("N1=");
      scanf("%d",&n1);
      printf("N2=");
      scanf("%d",&n2);
      head1=(node *)malloc(sizeof(node));
      scanf("%d",&head1->data);
      p=head1;
      for(i=1;i<n1;i++)
      {
            p->next=(node *)malloc(sizeof(node));
            p=p->next;
            scanf("%d",&p->data);
            p->next=NULL;
      }
      p=head1;
      printf("\n\nThe linked list 1 is=\n\nHead1");
      while(p!=NULL)
      {
            printf("->%d",p->data);
            p=p->next;
      }
      printf("\n\nEnter 2 linked list=\n");
      head2=(node *)malloc(sizeof(node));
      scanf("%d",&head2->data);
      p=head2;
      for(i=1;i<n2;i++)
      {
            p->next=(node *)malloc(sizeof(node));
            p=p->next;
            scanf("%d",&p->data);
            p->next=NULL;
      }
      p=head2;
      printf("\n\nThe linked list 2 is=\n\nHead2");
      while(p!=NULL)
      {
            printf("->%d",p->data);
            p=p->next;
      }
      p=head1;
      while(p->next!=NULL)
      p=p->next;
      p->next=head2;
      printf("\n\nThe conacatenated linked list is=\n\nHead");
      p=head1;
      while(p!=NULL)
      {
            printf("->%d",p->data);
            p=p->next;
      }
      getch();
}
     

      

No comments:

Post a Comment