Tuesday, March 31, 2015

Merge in order list 1 list 3 list 2

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
   int data;
  struct node * next;
};

void append(struct node **q)
{
 struct node *temp;
    temp=*q;
   if(temp==NULL)
  {
   *q=(struct node *)malloc(sizeof(struct node));
    printf("\nenter the data");
   scanf("%d",&(*q)->data);
   (*q)->next=NULL;
}
else
{
  while(temp->next!=NULL)
   temp=temp->next;

   temp->next=(struct node *)malloc(sizeof(struct node));
    temp=temp->next;

  printf("\nenter the data");
    scanf("%d",&temp->data);
  temp->next=NULL'
  }
}

void merge(struct node **q,struct node **q2,struct node *q3)
{
 struct node *temp,*t,*t1;
  temp=*q;
  t=*q2;
  t1=*q3;
  p=temp; //saving initial address of list 3

while(t1->next!=NULL)
     t1=t1->next;
   t1->next=t;

  while(temp->next!=NULL)
    temp=temp->next;
  temp->next=p;

}

void display(struct node *q)
{
  struct node *temp;
  temp=q;
    while(temp!=NULL)
    {
      printf(" %d",temp->data);
      temp=temp->next;
     }
}

void display(struct node *q)
{
  struct node *temp;
  temp=q;
    while(temp!=NULL)
    {
      printf(" %d",temp->data);
      temp=temp->next;
     } }

void main()
{
 struct node *start,*start2;
 start=NULL; //empty list1
start2=NULL; //empty list 2
start3=NULL; //empty list 3
  printf("\nenter the first list : ")
 append(&start);
 append(&start);
 append(&start);
printf("\now the list is :");
display(start);
printf("\nenter the second list :");
append(&start2);
 append(&start2);
 append(&start2);
printf('\now the list is :')
display(start2);
printf("\nenter the Third list :");
append(&start3);
 append(&start3);
 append(&start3);
printf('\now the list is :')
display(start3);
 merge(&start,&start2);
prinf("\nlist in order List3 list1 list 2");
display(&start);
}

No comments:

Post a Comment