Solved

linked of objects

Posted on 2004-04-09
4
200 Views
Last Modified: 2012-05-04
how to form a linked list of objects without using structures in the declaration of class. So a class object should point to an other object of the same class.
Ex
class temp
{
    int input;
   temp *next;
public:
  getvalue();
putvalue(0;
addnext(0;
};
0
Comment
Question by:rmvprasad
  • 2
  • 2
4 Comments
 

Author Comment

by:rmvprasad
ID: 10795793
I dont want to use templates. Thank you.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10795817
class Data {
          int myData ;
           Data *next ;
      public :
           Data ( int data = -1 , Data *link = NULL ) : myData ( data ), next ( link ) {}
} ;

class List {
        Data *list ;
    public :
        List () : list ( NULL ) {} ;
       void AddElementToList ( int dataElementToAdd ) ;
       void DisplayList () ;                
} ;

 In     void AddElementToList ( int dataElementToAdd )
You should implement adding a new element by using Data Constructor

Amit





0
 

Author Comment

by:rmvprasad
ID: 10795848
could you please gibe an sample code that too "You should implement adding a new element by using Data Constructor"
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 30 total points
ID: 10795900
AddElementToList ( int dataElementToAdd )  {

       Data *newNode = Data ( dataElementToAdd, NULL ) ;

       if ( list == NULL ) {
              list = newNode ;
       }      
      else {
            while ( temp -> next != NULL ) {  
                  temp = temp -> next ;
            }
            temp -> next = newNode ;
      }      
}


Amit
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question