C++ program to merge to linked list. Here it is required that you enter the linked lists which are already sorted in increasing order.
C++ program to merge two linked list
#include<iostream>
#include<conio.h>
using namespace std;
struct links
{
int data;
links *next;
};
class linkedlist
{
links *start1,*start2,*start3;
public:
void create(links **);
void merge();
void display(links**);
links** getstart1()
{
return &start1;
}
links** getstart2()
{
return &start2;
}
links** getstart3()
{
return &start3;
}
};
void linkedlist::merge()
{
links *temp3,*temp1,*temp2;
int flag=1;
temp1=start1;
temp2=start2;
while(temp1!=NULL)
{
if(temp1->data<temp2->data)
{
if(flag)
{
start3=new links;
temp3=start3;
temp3->data=temp1->data;
flag=0;
}
else
{
temp3->next=new links;
temp3=temp3->next;
temp3->data=temp1->data;
}
temp1=temp1->next;
}
else if(temp1->data>temp2->data)
{
if(flag)
{
start3=new links;
temp3=start3;
temp3->data=temp2->data;
flag=0;
}
else
{
temp3->next=new links;
temp3=temp3->next;
temp3->data=temp2->data;
}
temp2=temp2->next;
}
if(temp1==NULL)
{
while(temp2!=NULL)
{
temp3->next=new links;
temp3=temp3->next;
temp3->data=temp2->data;
temp2=temp2->next;
}
temp3->next=NULL;
}
if(temp2==NULL)
{
while(temp1!=NULL)
{
temp3->next=new links;
temp3=temp3->next;
temp3->data=temp1->data;
temp1=temp1->next;
}
temp3->next=NULL;
}
}
}
void linkedlist::create(links **start)
{
int take;
links *temp;
char ch;
cout<<"Enter the element ";
cin>>take;
temp=new links;
temp->data=take;
temp->next=NULL;
*start=temp;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
while(ch!='n'&&ch!='N')
{
cout<<"Enter the element ";
cin>>take;
temp->next=new links;
temp=temp->next;
temp->data=take;
temp->next=NULL;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
}
}
void linkedlist::display(links**start)
{
links *temp=*start;
while(temp!=NULL)
{
cout<<temp->data<<"-> ";
temp=temp->next;
}
cout<<"NULL\n";
}
int main()
{
linkedlist mergelist;
cout<<"Create a 1st list "<<endl;
mergelist.create(mergelist.getstart1());
cout<<"Create a 2nd list "<<endl;
mergelist.create(mergelist.getstart2());
cout<<"\nFirst linked list\n";
mergelist.display(mergelist.getstart1());
cout<<"\nSecond linked list\n";
mergelist.display(mergelist.getstart2());
mergelist.merge();
cout<<"\nAfter merging two linked list\n";
mergelist.display(mergelist.getstart3());
return 0;
}
#include<conio.h>
using namespace std;
struct links
{
int data;
links *next;
};
class linkedlist
{
links *start1,*start2,*start3;
public:
void create(links **);
void merge();
void display(links**);
links** getstart1()
{
return &start1;
}
links** getstart2()
{
return &start2;
}
links** getstart3()
{
return &start3;
}
};
void linkedlist::merge()
{
links *temp3,*temp1,*temp2;
int flag=1;
temp1=start1;
temp2=start2;
while(temp1!=NULL)
{
if(temp1->data<temp2->data)
{
if(flag)
{
start3=new links;
temp3=start3;
temp3->data=temp1->data;
flag=0;
}
else
{
temp3->next=new links;
temp3=temp3->next;
temp3->data=temp1->data;
}
temp1=temp1->next;
}
else if(temp1->data>temp2->data)
{
if(flag)
{
start3=new links;
temp3=start3;
temp3->data=temp2->data;
flag=0;
}
else
{
temp3->next=new links;
temp3=temp3->next;
temp3->data=temp2->data;
}
temp2=temp2->next;
}
if(temp1==NULL)
{
while(temp2!=NULL)
{
temp3->next=new links;
temp3=temp3->next;
temp3->data=temp2->data;
temp2=temp2->next;
}
temp3->next=NULL;
}
if(temp2==NULL)
{
while(temp1!=NULL)
{
temp3->next=new links;
temp3=temp3->next;
temp3->data=temp1->data;
temp1=temp1->next;
}
temp3->next=NULL;
}
}
}
void linkedlist::create(links **start)
{
int take;
links *temp;
char ch;
cout<<"Enter the element ";
cin>>take;
temp=new links;
temp->data=take;
temp->next=NULL;
*start=temp;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
while(ch!='n'&&ch!='N')
{
cout<<"Enter the element ";
cin>>take;
temp->next=new links;
temp=temp->next;
temp->data=take;
temp->next=NULL;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
}
}
void linkedlist::display(links**start)
{
links *temp=*start;
while(temp!=NULL)
{
cout<<temp->data<<"-> ";
temp=temp->next;
}
cout<<"NULL\n";
}
int main()
{
linkedlist mergelist;
cout<<"Create a 1st list "<<endl;
mergelist.create(mergelist.getstart1());
cout<<"Create a 2nd list "<<endl;
mergelist.create(mergelist.getstart2());
cout<<"\nFirst linked list\n";
mergelist.display(mergelist.getstart1());
cout<<"\nSecond linked list\n";
mergelist.display(mergelist.getstart2());
mergelist.merge();
cout<<"\nAfter merging two linked list\n";
mergelist.display(mergelist.getstart3());
return 0;
}
No comments:
Post a Comment