Hello My C++ is a little rusty but I have made a program that reverses a linked list and now I am trying to write the proper destructors for it but I don't know exactly what to destroy. Here are my class definitions:
class LinkedList
{
private:ListElement *start;
public:LinkedList();
public:void AddElement(int val);
public:void PrintList();
public:void InvertList();
};
class ListElement
{
public:int value;
public:ListElement * link;
public:ListElement(int val);
public:ListElement();
};
class Stack
{
private:ListElement ** stack;
private:int index;
public:Stack(int size);
public:void push(ListElement * le);
public:ListElement * pop();
};
The stack is for when I reverse the list. Anyway ... How would I go about writing the destructors for these? I was thinking:
For the ListElement make the value 0 and the link 0(NULL).
For the LinkedList go through the elements and call up the ListElementDestructor for all of them.
I am not very sure about this because as I understand the destructor automatically calls the destructors of the member objects so would only writing an empty destructor for LinkedList suffice in this case? I don't know ... that is why I am asking
For the stack I don't know ... the pointers are already 0(NULL) after the list is inversed because they are all poped.
I am a bit confused. Can anyone help? Thank you in advance.