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(); }
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);
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.
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
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?
adding two nodes without loops....................traverse.............. it,s an useful tip to reduce execution time..............
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(); }