merge two linklist
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *start1=NULL;
struct node *start2=NULL;
struct node *start3=NULL;
struct node * create_list(struct node *start)
{int n;
struct node *ptr;
printf("\n enter how many element you want to insert");
scanf("%d",&n);
while(n!=0)
{
n--;
printf("\n enter data");
ptr=(struct node*)malloc(sizeof(struct node));
scanf("\n%d",&ptr->data);
if(start==NULL)
{
start=ptr;
ptr->next=NULL;
}
else
{
ptr->next=start;
start=ptr;
}
}
return start;
}
void display(struct node *temp1)
{
struct node *temp;
temp=temp1;
if(temp==NULL)
{
printf("list is empty");
}
else
{
while(temp!=NULL)
{
printf("\t %d",temp->data);
temp=temp->next;
}
}
}
void merge(struct node *start1,struct node *start2)
{
int *d;
struct node *ptr,*temp,*temp1,*prev=NULL;
ptr=start1;
while(ptr!=NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=ptr->data;
temp->next=NULL;
if(start3==NULL)
{
start3=temp;
prev=temp;
}
else
{
prev->next=temp;
prev=temp;
}
ptr=ptr->next;
}
ptr=start2;
while(ptr!=NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=ptr->data;
temp->next=NULL;
if(start3==NULL)
{
start3=temp;
prev=temp;
}
else
{
prev->next=temp;
prev=temp;
}
ptr=ptr->next;
}
}
void main()
{
int c;
printf("\n 1 for insert data in first list");
printf("\n 2 for insert data in second list");
printf("\n 3 for display first list");
printf("\n 4 for display second list");
printf("\n 5 for merge list");
printf("\n 0 for exit");
while(1)
{
printf("\n enter your choice");
scanf("%d",&c);
switch(c)
{
case 1:
start1=create_list(start1);
break;
case 2:
start2=create_list(start2);
break;
case 3:
display(start1);
break;
case 4:
display(start2);
break;
case 5:
merge(start1,start2);
display(start3);
start3=NULL;
break;
case 0:
exit(1);
default:
printf("wrong choice");
}
}
}
struct node
{
int data;
struct node *next;
};
struct node *start1=NULL;
struct node *start2=NULL;
struct node *start3=NULL;
struct node * create_list(struct node *start)
{int n;
struct node *ptr;
printf("\n enter how many element you want to insert");
scanf("%d",&n);
while(n!=0)
{
n--;
printf("\n enter data");
ptr=(struct node*)malloc(sizeof(struct node));
scanf("\n%d",&ptr->data);
if(start==NULL)
{
start=ptr;
ptr->next=NULL;
}
else
{
ptr->next=start;
start=ptr;
}
}
return start;
}
void display(struct node *temp1)
{
struct node *temp;
temp=temp1;
if(temp==NULL)
{
printf("list is empty");
}
else
{
while(temp!=NULL)
{
printf("\t %d",temp->data);
temp=temp->next;
}
}
}
void merge(struct node *start1,struct node *start2)
{
int *d;
struct node *ptr,*temp,*temp1,*prev=NULL;
ptr=start1;
while(ptr!=NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=ptr->data;
temp->next=NULL;
if(start3==NULL)
{
start3=temp;
prev=temp;
}
else
{
prev->next=temp;
prev=temp;
}
ptr=ptr->next;
}
ptr=start2;
while(ptr!=NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=ptr->data;
temp->next=NULL;
if(start3==NULL)
{
start3=temp;
prev=temp;
}
else
{
prev->next=temp;
prev=temp;
}
ptr=ptr->next;
}
}
void main()
{
int c;
printf("\n 1 for insert data in first list");
printf("\n 2 for insert data in second list");
printf("\n 3 for display first list");
printf("\n 4 for display second list");
printf("\n 5 for merge list");
printf("\n 0 for exit");
while(1)
{
printf("\n enter your choice");
scanf("%d",&c);
switch(c)
{
case 1:
start1=create_list(start1);
break;
case 2:
start2=create_list(start2);
break;
case 3:
display(start1);
break;
case 4:
display(start2);
break;
case 5:
merge(start1,start2);
display(start3);
start3=NULL;
break;
case 0:
exit(1);
default:
printf("wrong choice");
}
}
}
Comments
Post a Comment