Wednesday, April 8, 2015

Merge to Nodes in Doubly Link List

#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(*q==NULL)
{
 *q=(struct node *)malloc(sizeof(struct node));
 printf("\n***list is empty**\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 another node :");
 scanf("%d",&temp->data);
 temp->next=NULL;
 }
 printf("\nnode created successfully");
}


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;
clrscr();
start=NULL;
start2=NULL;
 append(&start);
 append(&start);
printf("\nfirst link list");
display(start);

append(&start2);
append(&start2);

 printf("\n\nsecond link list");
display(start);

printf("\nmerge ");
merge(&start,&start2);
display(start);
 getch();
}

No comments:

Post a Comment