Deleting node from doubly linked list
Hi :)
I am writing a program in which events are entered into a list (no particular order). As the event passes (it is based on time), the node is flagged for removal. If there are any nodes to be removed a variable "nodesToRemove" is set (elsewhere in the program), and once all of the nodes that are flagged are removed from this list, it is reset to false (i.e. nothing to be removed). The problem I am having is that I cannot remove the node correctly. The node doesn't appear to be removed, with the exception of the time member which was allocated memory as well as the node that encloses it.
The "head" and "tail" (not used here) are private members of the class and are of type EVENT_NODE*.
Any ideas how to tidy this up?
Code:
int CEventTime::removeNodes(void)
{
EVENT_NODE *current;
EVENT_NODE *temp;
int numOfNodesRemoved = 0;
current = head;
while(current != NULL)
{
if (current->flaggedToRemove)
{
temp = current->next;
if( current->prev )
current->prev->next = current->next;
if( current->next )
current->next->prev = current->prev;
delete current->time; // This seems to work
delete current; // This does not!
// Update the counters
numOfNodesRemoved++;
numberOfEvents--;
}
current = temp;
}
nodesToRemove = false; // There are no further nodes to be removed
return numOfNodesRemoved;
}