Okay, I'm trying to have a linked list autonomously delete itself, however, the destructor function I am using doesn't seem to work, it just keeps constantly looping, until the program just ends itself.

Here's the function:

node::~node()
{
	if(this->next != NULL){delete this->next;}
	delete this;
}

Basically, this should run through the linked list, activating each ones destructor until the last one, then they will all delete themselves from the back up.
Now, I know that the more orthodox method is to have two pointers, one to delete and one as a temporary pointer to the next node, however, this seems like a better method that uses less variables, does anyone know why it isn't working though?

Surely you don't need to call:

delete this;

at the end of the destructor. You are in the destructor because delete has already been called. Calling it again creates the recursion.

Well at the first place . When the program run is over the OS itself deletes the linked list . so actually no need of deleting it using a destructor.

if u want to delete a link list all you have to do is free the first node of the list thats it .
if u have allocated the node using mallloc/calloc function then you can use free function to free the node or else
if you have new operator then you can use delete operator to fee the node.

Well at the first place . When the program run is over the OS itself deletes the linked list . so actually no need of deleting it using a destructor.

If you've allocated nodes in the list using new, as I presume has been done here, then you have to delete them yourself, that means ALL of them. If you don't, the program will exit with memory leaks.
Maybe you're thinking of a specific list implementation which will do that for you (much like the one Nishinoran is trying to implement in fact).

fine then by freeing the first node . we can delete the complete list .

No, you only delete the first node, the proper way to do it is:

while(node->next != NULL){  //node is the start of your list
rec = node;                            //rec is used to keep track of node
node = node->next;              //move node to next link
delete rec;                             //delete previous link
}

remeber the lists are still assigned to a junk array so they will not equal NULL, but the memory is freed.

No, you only delete the first node, the proper way to do it is:

while(node->next != NULL){  //node is the start of your list
rec = node;                            //rec is used to keep track of node
node = node->next;              //move node to next link
delete rec;                             //delete previous link
}

remeber the lists are still assigned to a junk array so they will not equal NULL, but the memory is freed.

Sorry but I don't get that. You say you should only delete the first node, but then you show it being done in a loop which explicitly deletes all nodes? Am I missing something?
Nishinoran wants to achieve the same result but put the deletion in the node destructor. Then the first node is explicitly deleted by the list client, as you indicate, and the node destructor takes care of the rest of the list.

This should take care of it for you as long as you have a head pointer which I'm assuming you do otherwise how would you access your list?

~CList() {
	// Create a tmp node
	CNode node;
	while(node->next != NULL) {
		node = head->next;
		delete head;
		head = node;
	}
}

Sorry but I don't get that. You say you should only delete the first node, but then you show it being done in a loop which explicitly deletes all nodes? Am I missing something?
Nishinoran wants to achieve the same result but put the deletion in the node destructor. Then the first node is explicitly deleted by the list client, as you indicate, and the node destructor takes care of the rest of the list.

I wasn't clear, it was a response to rahul; the snippet does exactly what you said it does.

rahul, I know that if I close it everything is erased, however, if I plan on having the program loop at all, this creates issues each time it loops, as I only delete the head pointer, and I leave the tail trailing, which ends up building up in memory, hence making a destructor like this.

And thanks Spigot, that was it, I don't know why I did that in the first place, it works perfectly now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.