Solved

linked of objects

Posted on 2004-04-09
4
202 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 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