How to create Doubly Link List without Loop.

Discussion in 'C++' started by Shishir191, Jul 26, 2007.

  1. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    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();
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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);
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Probably you meant in the Add function. Am I right?
     
  4. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    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.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Yeah I realized your point in the article after making the comment.
     
  6. loisdienmi

    loisdienmi New Member

    Joined:
    Jul 26, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    make make some $$$$

    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
     
  7. msdnguide

    msdnguide New Member

    Joined:
    May 14, 2011
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    msdnguide
    Location:
    Delhi
    Home Page:
    http://www.msdnguide.info/
    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?
     
  8. fashionbop

    fashionbop New Member

    Joined:
    Sep 11, 2009
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Shopping from China
    Location:
    Guangzhou, China
    Home Page:
    http://www.fashion-bop.com
    Oh ,I know your mean,"do not to traversal and find the last point"?
     
  9. anoopseo

    anoopseo New Member

    Joined:
    Jul 19, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    adding two nodes without loops....................traverse..............
    it,s an useful tip to reduce execution time..............
     
  10. William Tunnel

    William Tunnel New Member

    Joined:
    Aug 18, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Aug 19, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice