I'm working on an assignment and there's a problem I'm stuck on. So I'm making a doubly linked list. I want a a delete function that will take item as argument, search for that argument in the list. when it has found the node which contains that item, I have to DELETE that node. I am aware of how I would change the previous and next pointers to the the nodes around that node. The problem that has been bugging me however, is that when I just change the next pointer of the node before it and the previous pointer of the node after it, like in the code below, the particular node will only be disconnected from the list but it will still remain in the freestore. How do I delete it from there so that the memory it is taking is also freed?
The following is the code I have. Please take a look:
template <class T>
void LinkedList<T>::deleteElement(T item)
{
ListItem<T> *curPtr;
curPtr = searchFor(item); // this function returns the pointer to the node which contains the item.
(curPtr->next)->prev = curPtr->prev;
(curPtr->prev)->next = tempPtr->next;
}
So you see, the curPtr is being disconnected, but I believe it still exists somewhere on the freestore. How do I get rid of it permanantly?
Aucun commentaire:
Enregistrer un commentaire