PROGRAM TO MERGE TWO LISTS.
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
class Chain;
class Node
{
private:
int Info;
Node *Link;
friend class Chain;
};
class Chain
{
private:
Node *First;
public:
Chain()
{
First = NULL;
}
void Insert(int No)
{
Node *Temp = new Node;
Temp->Info = No;
Temp->Link = First;
First = Temp;
}
void Merge_List(Chain Ptr)
{
Node *Temp;
for(Temp = Ptr.First; Temp != NULL; Temp = Temp->Link)
Insert_End(Temp->Info);
}
void Insert_End(int Value)
{
Node *Temp, *Temp1;
Temp = First;
Temp1 = new Node;
Temp1->Info = Value;
Temp1->Link = NULL;
if(First == NULL)
First = Temp1;
else
{
while(Temp->Link != NULL)
Temp = Temp->Link;
Temp->Link = Temp1;
}
}
void Print()
{
if(ListMsg())
{
cout << "\nLIST UNDERFLOWED...!!!\n" << endl;
exit(1);
}
cout << "Elements in Node Is: " <<endl;
for(Node *Temp = First; Temp != NULL; Temp = Temp->Link)
cout << Temp->Info << endl;
}
inline bool ListMsg()
{
return First == NULL;
}
};
int main()
{
Chain Obj1, Obj2;
Obj1.Insert(12);
Obj1.Insert(13);
Obj1.Insert(14);
Obj1.Insert(15);
Obj1.Insert(16);
Obj2.Insert(17);
Obj2.Insert(18);
Obj2.Insert(19);
Obj2.Insert(20);
Obj2.Insert(21);
cout << "\n2nd List has been Merged To 1st List......\n" << endl;
Obj1.Merge_List(Obj2);
Obj1.Print();
getch();
return 0;
}
0 comments:
Post a Comment