How to create Doubly Link List without Loop.

Shishir191's Avatar author of How to create Doubly Link List without Loop.
This is an article on How to create Doubly Link List without Loop. in C++.
Rated 5.00 By 1 users
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();
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Shishir191
This example shows how to create the linklist without Loop.
What you meant by without a loop. You are using the loop in the traversal as well inserting them as
Code:
Obj.Add(1);
Obj.Add(2);
Obj.Add(3);
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by shabbir
What you meant by without a loop. You are using the loop in the traversal as well inserting them as
Code:
Obj.Add(1);
Obj.Add(2);
Obj.Add(3);
Probably you meant in the Add function. Am I right?
0
Shishir191's Avatar, Join Date: Jul 2007
Go4Expert Member
Hi,
I mean there is no need to use any for or while loop for the addition of new node.
Even there is no need to traverse the whole link list because i have stored the address of the last node in the "Last" pointer.

For Ex:

while(Temp->Nxt != NULL)
Temp = Temp->Nxt;

In the above example if you want to add any new node you have to traverse again and again until you do not found the last node.

But in my example there is no need to traverse.

when i called

Obj.Add(1);
Obj.Add(2);
Obj.Add(3);

In this case when second or third time Add function called then it adds the node after the Last Node but traversal is not here.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Yeah I realized your point in the article after making the comment.
0
loisdienmi's Avatar
Light Poster
You share ..i share..let put hand together and make money.......


I person deal with CC,Cvv and Logins...for $$$...hope you understand..if you gat me ..then maybe we can work together and make DOOOOOOO

add me on Y..loisdienmi
Leas
0
msdnguide's Avatar, Join Date: May 2011
Go4Expert Member
if you have to add 100000 elements, then what you will do....you can create even singly linked list without a loop. I mean whats great in this code I m not able to get. This is the standard way of dping it. Can you explain where you needed to create a loop and how you are avoiding it?
0
shzlw's Avatar, Join Date: Jul 2011
Light Poster
Oh ,I know your mean,"do not to traversal and find the last point"?
0
anoopseo's Avatar, Join Date: Jul 2011
Newbie Member
adding two nodes without loops....................traverse..............
it,s an useful tip to reduce execution time..............
0
William Tunnel's Avatar, Join Date: Aug 2011
Newbie Member
This example shows how to create the linklist without Loop.

Code:
#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();
}

Last edited by shabbir; 19Aug2011 at 13:46.. Reason: Code blocks