delete LinkedList from pointer

Sup?

let's say I have class LinkedList:
1
2
3
4
5
6
7
8
9
10
11
12
13
class LinkedList
{
public:
	class Node
	{
	public:
		Data* data;
		Node* next;
	}; 
	Node* head;
public:
       // methods.....
};

I do the following:
LinkedList* newL = new LinkedList() // constructor: LinkedList() { head = NULL; }
Now I do things like add thing to newL:
1
2
3
newL->Add(x)
newL->Add(y)
newL->Add(z)

Now I want to delete newL. I can I do it?
This is the code of the D'tor:
1
2
3
4
5
6
7
8
9
10
11
LinkedList::~LinkedList()  
{
	Node* current = head;
	while( current != NULL )
	{
		Node* next = current->next;
		delete current;
		current = next;
	}
	head = NULL;
}


Do delete newL:
1) (*newL).~LinkedList
2) delete newL
3) delete[] newL
4) none of the above ;)

If the answer is 4, what should I do?

any help will be appreciated.
Last edited on
http://www.cplusplus.com/doc/tutorial/dynamic/

What does newL->Add(x) do?
Your Node has a pointer to Data. Who owns that memory?
hi :)

newL->Add(x) add Data* x to newL::Node*->Data

so, in the end, I have got the following:

newL->head
|
v
--------- --------- ---------
| x | --+---> | y | --+---> | z | 0 |
--------- --------- ---------


my question is: how can I delete the above?
someone plz?
2) is ok
Yes, (2) calls the destructor of newL and then releases the memory used by that LinkedList object. The destructor removes each Node object that was on the list. What remains uncertain is what do the Add and ~Node members of each Node do to the Data *.
Topic archived. No new replies allowed.