Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

destructing a linked class!! 1

Status
Not open for further replies.

ankan

Programmer
Jun 22, 2001
70
US
Hi

I wanted to make a doubly linked class (like a linked list but with member functions too). The question is should the destructor delete all the objects that are linked, or just that particular object.

:-I help. Ankan.

Please do correct me if I am wrong. s-)
 
The usual thing to do when creating double linked lists is to create a container for the linked list, with pointers to the first and the last element of the list, something like:


Code:
class Member {
private:
   void *data;
   Member *prev,*next;

public:
   Member(){
      data = NULL; // other initializing
   };
   ~Member()
       {  if (data)
            delete data;  
       };
 // some functions
};   

class Container {    
  private:
      Member *first,*last;
  public:
      Container(); //... initialization code
      ~Container() {
          Member *it;
          for (it = first; (it != NULL); it = it->next)
           delete it;
      };
};
This would be my usual approach.

You can use also the <list> STL template which provides you with all the functionality you need.

Hope this helps...

Nosferatu
We are what we eat...
There's no such thing as free meal...
 
I think it is preferable to get and study more
informations on data structures - books or internet
articles. You should understand linked list perfectly
if you want to upgrade your ability. Because linked
list is the root of data structure.
Hee S. Chung
heesc@netian.com
 
Thanks Nosferatu. It did help but, I'm still a bit confused... so u all can surely help me out...

Shouldn't the container objects 'first' and 'last' be public? How else am i supposed to refer to them from outside.:-I
And, maybe the container destructor should be ...
~Container() {
Member *it, *last_it;
for (it = first; (it != NULL); it = last_it) {
last_it = it->next;
delete it;
}
delete last_it;
};
... because in Nosferatu's code is referring to 'it' in the for loop after deleting it. Hope I'm correct.

As an example say I have a class
class student {
char*name; int *marks;/*...*/ student *prev, *next;
public:
void SortByName();
void SortByMarks(); // etc....
};
Do I have to use a container? If so, how?

I tried searching for articles on linked lists, data structures and container classes in the net. Can any of u please provide a link (apart from Ankan.

Please do correct me if I am wrong. s-)
 
Well, Ankan, these are questions you would rather put in the C++ forum, and you would find some helpfull info there.
Anyway.
The reason i made first/last members private is for implementation hiding , a concept widely used (a basic concept actually) in C++. The idea is that you provide some functions to access the private members of a class (read and/or write them) insteat letting the programmer change the first/last members at their will. This ensures you that nobody will be able to crack the class if they want to or by mistake... The only one who can break the class to pieces (and the computer along with it) is... you. So, the data used by a class should be manipulated directly by you and only you. Of course, if somebody really wants to freeze a computer or create GPF's of SegFaults... he/she will regardless of your implementation hiding, but that's off the line here...

A great book for C++ is Bruce Eckels &quot;Thinking in C++&quot;, 2nd edition being the latest release. I used this book (and using it, since i'm a kindof beginner in C++ myself) and its great. You can download it at :
Just follow the links...

Hope this helps... Nosferatu
We are what we eat...
There's no such thing as free meal...
 
Of course, data hiding, encapsulation, implementation hiding etc. etc.... are the basics.

But you are already doing that in the member class. So that only the functions of the member class have access to the data. What is the point of doing that again in the container class?

Morever functions in the member class are already doing whatever is needed with the data (say sorting, etc.). Writing functions in the container class that just connect to the functions in the member class doesn't have much of a point. Right?

Or is it conventional to put the data in the member class, and all the functions in the container class? Then the data has to be accessible to the container class making it necessary to make them public - going against the basics of implementation hiding. So that's not an option.

Finally, maybe a function in the container that returns the values of prev/next is the way to go.

I'd thought of putting the question in a C++ forum, but the problem is that this site doesn't have one on general C++. The Borland C++/ MS C++ are more or less specific. The OOP forum is not really visited much, and also quite general. C++ Unix was an option though. Anyway, most C++ users visit C too, so... :) Will try to keep C++ Qs away from C next time. But since this has already been asked... forgive me.

Thanks for the link. Will check it out.... Ankan.

Please do correct me if I am wrong. s-)
 
Hey Ankan!

I was just making a sugestion about the C++ forum, i'm sorry if it sounded as an accusation or something.

I can understand you confusion, it happened to me as well, and it generally happens to anyone that &quot;migrates&quot; from C to C++, which i understand is your case as well. A lot of C++ stuff will sound useless to you (like making first-last members private) untill you begin to think in a OO manner. Like i said, i had these problems myself, so you will probably spend some time familiarizing with the idea of OO.

It's a good point that you mentioned the OOP forum which should me more visited that it is now.

First of all, i appologize for the huge mistake i made in the destructor!
Second:
The idea is to separate as much as possible the objects and specify them a strictly related behavior.
This would mean that in the Member class you only should manipulate a Member object, that is setting, retrieving data and [red]not sorting the list the member it belongs to[/red]!
Thats the container's business... The container will implement behavior specific to what a container should do, that is adding, replacing, deleting, sorting data.
DIVIDE ET IMPERA.

Third of all:
Why make first and last private if the member class already has private members?
Well... This is somehow a reccurent question...
Because first & last belong to the Container class, so you simply don't want to let anybody to modify them/
Why let somebody say:
Code:
member = NULL;  // somewhere in the code...
container->first = member;
or setting unintentionally that value to NULL when you can say:
Code:
member = NULL;  // somewhere in the code...
container->setFirst(member);

// and then, in the container implementation:
Member * Container::setFirst(Member * node)
{  Member ret = node;
   if (node)
   {   ret = first;  
       first = node;
   }
   return ret;
}

Here, for example, the setFirst function returns the previous first element or NULL.

Hope this helps a little bit. Again, i appologize if i am making some mistakes and welcome anybody to correct me.
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
Thanks a lot!! Nosferatu.

The concept of Containers was new to me, and your replies above were very helpful. They really helped (and hence the stars :) ).

BTW, the destructor that I gave above need not have the line
delete last_it;
and if it has it, it should be
if(last_it) delete last_it;
That's what my brain says after its latest update!! LOL

Just started downloading that book... will bother u again if I have to ;-) (hope u would be kind enough to answer again)...

Do any of u know any links to books specifically on Linked Lists, Data Structures, Trees, etc. on the net? please :-I??
Ankan.

Please do correct me if I am wrong. s-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top