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.

Second Year Computer Engineering Data Structure Programs:

Data Structure Lab:

PracticalB20:

Let x = (x1,x2, … , xn) and y = (y 1, y2,…. , ym) 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<iostream>
#include<stdlib.h>
#include<iostream>
#include<string.h>
using namespace std;
struct node
{ int data;
node *next;
node *prev;
}
*head3,*head1,*head2,*head4;
class linklist
{  public:
int count1,count2;
void create()
{ count1=0;
count2=0;
head1=NULL;
head2=NULL;
head3=NULL;
head4=NULL;
}
void intake(int val)
{ node *p,*q;
p=new node;
if(head1==NULL)
{   head1=new node;
head1->data=val;
head1->next=NULL;
head1->prev=NULL;
count1++;
}
else
{ p=head1;
while(p->next!=NULL)
p=p->next;
q=new node;
q->data=val;
q->next=NULL;
q->prev=p;
p->next=q;
q->next=NULL;
count1++;
}
}
void intake1(int val)
{ node *p,*q;
p=new node;
if(head2==NULL)
{   head2=new node;
head2->data=val;
head2->next=NULL;
head2->prev=NULL;
count2++;
}
else
{ p=head2;
while(p->next!=NULL)
p=p->next;
q=new node;
q->data=val;
q->next=NULL;
q->prev=p;
p->next=q;
q->next=NULL;
count2++;
}
}
void display()
{ node *p;
p=new node;
p=head1;
cout<<"this is the x linklist"<<endl;
while(p!=NULL)
{ cout<<p->data<<endl;
p=p->next;
}
node *q;
q=new node;
q=head2;
cout<<"this is the y linklist"<<endl;
while(q!=NULL)
{ cout<<q->data<<endl;
q=q->next;
}
}
void rearrange()
{ int i,temp;
node *p;
p=new node;
p=head1;
node *q;
q=new node;
while(p!=NULL)
{   q=head1;
while(q!=NULL)
{ if(p->data<q->data)
{   temp=p->data;
p->data=q->data;
q->data=temp;
}
q=q->next;
}
p=p->next;
}
p=head2;
while(p!=NULL)
{      q=head2;
while(q!=NULL)
{ if(p->data<q->data)
{   temp=p->data;
p->data=q->data;
q->data=temp;
}
q=q->next;
}
p=p->next;
}
node *t;
t=new node;
if(head1->data<head2->data)
{ head4=p=head1;
head1=head1->next;
}
else
{ head4=p=head2;
head2=head2->next;
}
while(head1!=NULL&&head2!=NULL)
{ if(head1->data<head2->data)
{ p->next=head1;
head1->prev=p;
head1=head1->next;
p=p->next;
}
else
{
{ p->next=head2;
head2->prev=p;
head2=head2->next;
p=p->next;
}
}
}
if(head1!=NULL)
{ p->next=head1;
head1->prev=p;
}
if(head2!=NULL)
{ p->next=head2;
head2->prev=p;
}
head1=head2=NULL;
}
void display3()
{ node *p;
p=new node;
p=head4;
cout<<"this is the z"<<endl;
while(p!=NULL)
{ cout<<p->data<<endl;
p=p->next;
}
cout<<"count1"<<count1<<count2;
}
};
int main()
{ char y='y',x='y';
     linklist a;
   do
   {  a.create();
      while(y=='y')
      {   cout<<"enter the number u want to store in link list x"<<endl;
   int num;
   cin>>num;
   a.intake(num);
    cin>>y;
 }
      y='y';
    while(y=='y')
    {   cout<<"enter the number u want to store in link list y"<<endl;
   int num;
   cin>>num;
      a.intake1(num);
  cin>>y;
}
    a.display();
    a.rearrange();
    a.display3();
     cout<<"do u want to continue(y/n)";
     cin>>x;
    }while(x=='y');
   return 0;
}
----------------------------------------------------------------------------------------------------------------------------------

/************************************output*****************************
enter the number u want to store in link list x
2
1
enter the number u want to store in link list y
3
4
this is the x linklist
2
this is the y linklist
3
this is the z
2
3
count111do u want to continue(y/n)y
enter the number u want to store in link list y
3
4
this is the x linklist
this is the y linklist
3
****************end*****************************/

Comments

Popular posts from this blog

Implement C++ program for expression conversion as infix to postfix and its evaluation using stack.

Write C++ program to maintain club member‘s information using singly linked list. Store student PRN and Name.

Write C++/Java program for line drawing using DDA or Bresenhams algorithm with patterns such as solid, dotted, dashed, dash dot and thick.