Solved

linked of objects

Posted on 2004-04-09
4
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

730 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