I am trying to write a delete algorithm for an ordered linked list. Search and traverse I think I have down but delete is giving me fits. It crashes every time. Any help is greatly appreciated!
I have a private pointer to a ListNode strcuture called head in my TestLL class. The ListNode structure contains int Key, double dataValue, and ListNode *next. The function returns true if the node to delete was found and deleted or false if it was not found.
bool TestLL::Delete(int Key)
{
ListNode *back = NULL, *temp = head;
//Search for the node to delete
while((temp != NULL) && (key != temp -> key))
//advance the pointers
back = temp;
temp = temp->next;
}
//Check for node to delete not being found
if( temp == NULL)
{
returnfalse;
}
//Check for deleting the head of the list
elseif( back == NULL) //I'm very unsure of my else if and else
{
delete temp;
}
else// Remove node other than at the head
{
delete temp;
}
//deallocate memory used by the removed node
free(temp);
returntrue;
}
bool TestLL::Delete(int Key)
{
ListNode *back = NULL, *temp = head;
//Search for the node to delete
while((temp != NULL) && (key != temp -> key))
{
//advance the pointers
back = temp;
temp = temp->next;
}
//Check for node to delete not being found
if( temp == NULL)
{
returnfalse;
}
//Check for deleting the head of the list
elseif( back == NULL) //I'm very unsure of my else if and else
{
delete temp;
}
else// Remove node other than at the head
{
delete temp;
}
//deallocate memory used by the removed node
free(temp);
returntrue;
}
Is this your problem? I can't imagine that it would have compiled as-is so if it's not a syntax mistake, I'll need to look closer.
Thanks for the replies. I'm not sure what command to use to remove/ delete nodes besides delete. Also the deallocating memory syntax bit at the end is confusing me.
delete just destructs the node and releases the memory for it, but you still need to change your linked list so it no longer is pointing at the memory of the node you just destructed.
Actually, a second look made me think about your elseif. There is only one way that back would be NULL. That case is when temp is NULL from the start. If that does happen then deleting temp would result in a problem because there is nothing to delete.
bool TestLL::Delete(int Key)
{
ListNode *back = NULL, *temp = head;
//Search for the node to delete
while((temp != NULL) && (key != temp -> key))
{
//advance the pointers
back = temp;
temp = temp->next;
}
//Check for node to delete not being found
if( temp == NULL)
{
returnfalse;
}
//Check for deleting the head of the list
elseif( back == NULL) //I'm very unsure of my else if and else
{
returnfalse;
}
else// Remove node other than at the head
{
delete temp;
}
//deallocate memory used by the removed node
free(temp);
returntrue;
}
Right, I hadn't refreshed the page to see your comments in a while. Also, we don't really need put else if after a return statement, so here's something to make the code a little shorter too:
bool TestLL::Delete(int Key)
{
ListNode *back = NULL, *temp = head;
//Search for the node to delete
while((temp != NULL) && (key != temp -> key))
{
//advance the pointers
back = temp;
temp = temp->next;
}
//Check for node to delete not being found
if( temp == NULL)
returnfalse;
//Check for deleting the head of the list
if( back == NULL) //I'm very unsure of my else if and else
returnfalse;
//deallocate memory used by the removed node
delete temp;
returntrue;
}