PROGRAM TO CONCATENATE TWO LISTS TO 3rd
#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 Concate_List(Chain Ptr1, Chain Ptr2)
{
Node *Temp2;
for(Temp2 = Ptr2.First; Temp2 != NULL; Temp2 = Temp2->Link)
Insert(Temp2->Info);
Node *Temp1;
for(Temp1 = Ptr1.First; Temp1 != NULL; Temp1 = Temp1->Link)
Insert_End(Temp1->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, Obj3;
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 << "\n1st and 2nd list has been Concatenated to 3rd List......\n" << endl;
Obj3.Concate_List(Obj1, Obj2);
Obj3.Print();
getch();
return 0;
}
0 comments:
Post a Comment