PROGRAM TO IMPLEMENT THE DOUBLY LINKLIST.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
class Double_LL;
class Node
{
private:
int Info;
Node *Left, *Right;
friend class Double_LL;
};
class Double_LL
{
private:
Node *First;
public:
Double_LL()
{
First = NULL;
}
void First_Node(int Value)
{
Node *Temp = new Node;
Temp->Info = Value;
Temp->Right = NULL;
Temp->Left = NULL;
First = Temp;
}
void Insert_Right(int Value1, int Value2)
{
Node *R, *P, *Temp;
Temp = new Node;
Temp->Info = Value2;
P = Search(Value1);
R = P->Right;
if(P == NULL)
{
cout << "\nCannot Insert On Right Of It.....!!!\n" << endl;
exit(1);
}
Temp->Left = P;
P->Right = Temp;
Temp->Right = R;
if(R != NULL)
R->Left = Temp;
}
void Insert_Left(int Value1, int Value2)
{
Node *L, *P, *Temp;
Temp = new Node;
Temp->Info = Value2;
P = Search(Value1);
L = P->Left;
if(P == NULL)
{
cout << "\nCannot Insert On Left Of It.....!!!\n" << endl;
exit(1);
}
Temp->Right = P;
P->Left = Temp;
Temp->Left = L;
if(L != NULL)
L->Right = Temp;
}
void Remove(int Value)
{
Node *L, *R, *Temp;
Temp = Search(Value);
if(Temp == NULL || IsEmpty())
{
cout << "\nDeletion is Not Possible...!!!\n" << endl;
exit(1);
}
L = Temp->Left;
R = Temp->Right;
cout << "Value To Be Deleted is: " << Temp->Info << endl;
if(L == NULL)
R->Left = NULL;
else if(R == NULL)
L->Right = NULL;
else
{
L->Right = R;
R->Left = L;
}
}
Node* Search(int Value)
{
Node *Temp;
for(Temp = First; Temp != NULL ;Temp = Temp->Right)
{
if(Temp->Info == Value)
return Temp;
}
return NULL;
}
void Print()
{
Node *Temp = new Node;
for(Temp = First; Temp != NULL; Temp = Temp->Right)
cout << "Value Of Node Is " << Temp->Info << endl;
}
inline bool IsEmpty()
{
return First == NULL;
}
};
int main()
{
Double_LL Obj;
Obj.First_Node(1);
Obj.Insert_Right(1,2);
Obj.Insert_Right(2,3);
Obj.Insert_Right(3,4);
Obj.Insert_Right(4,5);
Obj.Insert_Left(5,6);
Obj.Insert_Left(6,7);
Obj.Insert_Left(7,8);
Obj.Insert_Left(8,9);
Obj.Remove(1);
Obj.Remove(2);
Obj.Remove(3);
Obj.Remove(4);
Obj.Remove(5);
Obj.Remove(6);
Obj.Remove(7);
///Obj.Print();
getch();
return 0;
}
0 comments:
Post a Comment