This example shows how to create the linklist without Loop.
Code: Cpp
#include<iostream.h>
#include<conio.h>
//Structure of the Doubly Link List Node.
// Data stores the value of type integer.
struct Node
{
int Data;
Node *Nxt; //Points to the Next Node
Node *Pre; //Points to the Previous Node
};
class Double_List
{
Node * Base; //Stores the base address of the link list
Node * Last; //Stores the last address of the link list.
public:
Double_List() //Constructor
{
Base = NULL;
Last = NULL;
}
~Double_List() //Destructor used to free the memory.
{
Node * Temp = NULL;
Temp = Base;
while(Temp)
{
Base = Temp->Nxt;
delete Temp;
Temp = Base;
}
}
void Add(int Val); //Create the link list and stores the value
void Display(); //It is used to display the result.
};
void Double_List::Add(int Num)
{
Node * Temp = NULL;
Temp = new Node; //Creates the new node.
Temp->Data = Num; //Temporary data stored in the Temp
Temp->Nxt = NULL;
if(Base == NULL) //Checks if the Base is Null means List is not yet created.
{
Base = Temp;
Base->Pre = NULL; //First time Previous is null.
Last = Base; //First time Last stores the address of the fist node.
}
else
{
Last->Nxt = Temp;
Temp->Pre = Last;
Last = Temp; //Last stores the address of the last node.
}
}
void Double_List::Display()
{
Node *Temp = NULL;
Temp = Base;
while(Temp) //Loop continues until it does not found null.
{
cout<<"Data is "<<Temp->Data<<endl;
Temp = Temp->Nxt;
}
}
int main()
{
clrscr();
Double_List Obj;
Obj.Add(1);
Obj.Add(2);
Obj.Add(3);
Obj.Display();
getch();
}

