Solved

linked of objects

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now