/*
Let x = (x 1 ,x 2 , ... , x n ) and y = (y 1 , y 2 ,.... , y m ) be two doubly linked lists. Assume that in
each linked list, the nodes are in non-decreasing order of their data-field values. Write
C/C++ program to merge the two lists to obtain a new linked list z in which the nodes are
also in this order. Following the merge, x and y should represent empty lists because each
node initially in x or y is now in z. No additional nodes may be used
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
struct node * prv;
};
int main()
{
int l1,l2,i;
printf("\nEnter total no. of node in list 1\n");
scanf("%d",&l1);
printf("\nEnter total no. of node in list 2\n");
scanf("%d",&l2);
struct node *list1=NULL;
i=0;
while(i<l1)
{
if(list1==NULL)
{
list1=(struct node *) malloc(sizeof(struct node));
printf("\nEnter the no. in list 1\n");
scanf("%d",&list1->data);
list1->next=NULL;
list1->prv=NULL;
}
else
{
struct node *temp;
temp=list1;
while(temp->next!=NULL)
temp=temp->next;
struct node *newnode=(struct node *) malloc(sizeof(struct node));
printf("\nEnter the no. in list 1\n");
scanf("%d",&newnode->data);
temp->next=newnode;
newnode->next=NULL;
newnode->prv=temp;
}
i++;
}
struct node *list2=NULL;
i=0;
while(i<l2)
{
if(list2==NULL)
{
list2=(struct node *) malloc(sizeof(struct node));
printf("\nEnter the no. in list 2\n");
scanf("%d",&list2->data);
list2->next=NULL;
list2->prv=NULL;
}
else
{
struct node *temp;
temp=list2;
while(temp->next!=NULL)
temp=temp->next;
struct node *newnode=(struct node *) malloc(sizeof(struct node));
printf("\nEnter the no .in list 2\n");
scanf("%d",&newnode->data);
temp->next=newnode;
newnode->next=NULL;
newnode->prv=temp;
}
i++;
}
printf("\nList 1\n");
struct node *temp;
temp=list1;
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->next;
}
printf("\nList 2\n");
temp=list2;
while(temp!=NULL){
printf("\t%d",temp->data);
temp=temp->next;
}
struct node *z=NULL;
while(list1!=NULL && list2!=NULL)
{
if(list1->data < list2->data)
{
if(z==NULL)
{
z=list1;
list1=list1->next;
z->next=NULL;
z->prv=NULL;
}
else
{
struct node *tt;
tt=z;
while(tt->next!=NULL)
tt=tt->next;
tt->next=list1;
list1->prv=tt;
list1=list1->next;
tt->next->next=NULL;
}
}
else
{
if(z==NULL)
{
z=list2;
list2=list2->next;
z->next=NULL;
z->prv=NULL;
}
else
{
struct node *tt;
tt=z;
while(tt->next!=NULL)
tt=tt->next;
tt->next=list2;
list2->prv=tt;
list2=list2->next;
tt->next->next=NULL;
}
}
}
while(list1!=NULL)
{
if(z==NULL)
{
z=list1;
list1=list1->next;
z->prv=NULL;
}
else
{
struct node *tt;
tt=z;
while(tt->next!=NULL)
tt=tt->next;
tt->next=list1;
list1->prv=tt;
list1=list1->next;
tt->next->next=NULL;
}
}
while(list2!=NULL)
{
if(z==NULL)
{
z=list2;
list2=list2->next;
z->prv=NULL;
}
else
{
struct node *tt;
tt=z;
while(tt->next!=NULL)
tt=tt->next;
tt->next=list2;
list2->prv=tt;
list2=list2->next;
tt->next->next=NULL;
}
}
printf("\nmerged list\n");
temp=z;
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->next;
}
return 0;
}
/*
output:
Enter total no. of node in list 1
4
Enter total no. of node in list 2
5
Enter the no. in list 1
1
Enter the no. in list 1
2
Enter the no. in list 1
3
Enter the no. in list 1
4
Enter the no. in list 2
5
Enter the no .in list 2
6
Enter the no .in list 2
7
Enter the no .in list 2
8
Enter the no .in list 2
9
List 1
1 2 3 4
List 2
5 6 7 8 9
merged list
1 2 3 4 5 6 7 8 9
*/
Let x = (x 1 ,x 2 , ... , x n ) and y = (y 1 , y 2 ,.... , y m ) be two doubly linked lists. Assume that in
each linked list, the nodes are in non-decreasing order of their data-field values. Write
C/C++ program to merge the two lists to obtain a new linked list z in which the nodes are
also in this order. Following the merge, x and y should represent empty lists because each
node initially in x or y is now in z. No additional nodes may be used
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
struct node * prv;
};
int main()
{
int l1,l2,i;
printf("\nEnter total no. of node in list 1\n");
scanf("%d",&l1);
printf("\nEnter total no. of node in list 2\n");
scanf("%d",&l2);
struct node *list1=NULL;
i=0;
while(i<l1)
{
if(list1==NULL)
{
list1=(struct node *) malloc(sizeof(struct node));
printf("\nEnter the no. in list 1\n");
scanf("%d",&list1->data);
list1->next=NULL;
list1->prv=NULL;
}
else
{
struct node *temp;
temp=list1;
while(temp->next!=NULL)
temp=temp->next;
struct node *newnode=(struct node *) malloc(sizeof(struct node));
printf("\nEnter the no. in list 1\n");
scanf("%d",&newnode->data);
temp->next=newnode;
newnode->next=NULL;
newnode->prv=temp;
}
i++;
}
struct node *list2=NULL;
i=0;
while(i<l2)
{
if(list2==NULL)
{
list2=(struct node *) malloc(sizeof(struct node));
printf("\nEnter the no. in list 2\n");
scanf("%d",&list2->data);
list2->next=NULL;
list2->prv=NULL;
}
else
{
struct node *temp;
temp=list2;
while(temp->next!=NULL)
temp=temp->next;
struct node *newnode=(struct node *) malloc(sizeof(struct node));
printf("\nEnter the no .in list 2\n");
scanf("%d",&newnode->data);
temp->next=newnode;
newnode->next=NULL;
newnode->prv=temp;
}
i++;
}
printf("\nList 1\n");
struct node *temp;
temp=list1;
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->next;
}
printf("\nList 2\n");
temp=list2;
while(temp!=NULL){
printf("\t%d",temp->data);
temp=temp->next;
}
struct node *z=NULL;
while(list1!=NULL && list2!=NULL)
{
if(list1->data < list2->data)
{
if(z==NULL)
{
z=list1;
list1=list1->next;
z->next=NULL;
z->prv=NULL;
}
else
{
struct node *tt;
tt=z;
while(tt->next!=NULL)
tt=tt->next;
tt->next=list1;
list1->prv=tt;
list1=list1->next;
tt->next->next=NULL;
}
}
else
{
if(z==NULL)
{
z=list2;
list2=list2->next;
z->next=NULL;
z->prv=NULL;
}
else
{
struct node *tt;
tt=z;
while(tt->next!=NULL)
tt=tt->next;
tt->next=list2;
list2->prv=tt;
list2=list2->next;
tt->next->next=NULL;
}
}
}
while(list1!=NULL)
{
if(z==NULL)
{
z=list1;
list1=list1->next;
z->prv=NULL;
}
else
{
struct node *tt;
tt=z;
while(tt->next!=NULL)
tt=tt->next;
tt->next=list1;
list1->prv=tt;
list1=list1->next;
tt->next->next=NULL;
}
}
while(list2!=NULL)
{
if(z==NULL)
{
z=list2;
list2=list2->next;
z->prv=NULL;
}
else
{
struct node *tt;
tt=z;
while(tt->next!=NULL)
tt=tt->next;
tt->next=list2;
list2->prv=tt;
list2=list2->next;
tt->next->next=NULL;
}
}
printf("\nmerged list\n");
temp=z;
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->next;
}
return 0;
}
/*
output:
Enter total no. of node in list 1
4
Enter total no. of node in list 2
5
Enter the no. in list 1
1
Enter the no. in list 1
2
Enter the no. in list 1
3
Enter the no. in list 1
4
Enter the no. in list 2
5
Enter the no .in list 2
6
Enter the no .in list 2
7
Enter the no .in list 2
8
Enter the no .in list 2
9
List 1
1 2 3 4
List 2
5 6 7 8 9
merged list
1 2 3 4 5 6 7 8 9
*/
No comments:
Post a Comment