Tuesday, July 28, 2009

Trouble with Delete function in c++?

Can someone help me with my delete function? I am having trouble figuring out why my delete function is not reporting false when attempting to delete a bank id that


does not exist. Thanks.





bool Bank::RemoveBankNumber(int bankID)


{


Bank *temp, *back;


if (head == NULL) return false;//(isEmpty()) return;


// Search the list for the item to delete


temp = head;


back = NULL;





while((temp != NULL) %26amp;%26amp; ( temp-%26gt;getbankID() != bankID ))


{


back = temp;


temp = temp-%26gt;next;


}


// Check to see if the item was found


if(temp == NULL) return false; // Not found so return false


else if(back == NULL) // Check to see if item is first in list


{


head = head-%26gt;next;


delete temp; // Dispose of the node removed from the list


}


else // Delete node elsewhere in the list


{


back-%26gt;next = temp-%26gt;next;


delete temp; // Dispose of the node removed from the list


}


return true; // Signal successful deletion


}

Trouble with Delete function in c++?
Situtations like this, I use the "poor man's debugger." That is, cout. You really want to know if execution made it to certain points or not. A return of True or False might not give the whole story. Anyways, like so:





while((temp != NULL) %26amp;%26amp; ( temp-%26gt;getbankID() != bankID ))


{


back = temp;


temp = temp-%26gt;next;


}


// Check to see if the item was found


if(temp == NULL)


{


cout %26lt;%26lt; "temp == NULL not found so returning false" %26lt;%26lt; endl;


return false; // Not found so return false


}


else if(back == NULL) // Check to see if item is first in list


{


cout %26lt;%26lt; "back == NULL" %26lt;%26lt; endl;


head = head-%26gt;next;


delete temp; // Dispose of the node removed from the list


}


else // Delete node elsewhere in the list


{


cout %26lt;%26lt; "back is NOT NULL ...hmmmm..." %26lt;%26lt; endl;


back-%26gt;next = temp-%26gt;next;


delete temp; // Dispose of the node removed from the list


}


cout %26lt;%26lt; "returning TRUE, right now!..." %26lt;%26lt; endl;


return true; // Signal successful deletion


}





Now watch your program output at run time. You might think you know what the execution path will be, but nothing proves it like cout or printf(). A thousand ills can be found and cured this way.





....cheers...
Reply:while((temp != NULL) %26amp;%26amp; ( temp-%26gt;getbankID() != bankID ))


{


back = temp;


temp = temp-%26gt;next;


}





This loop will stop if Temp is NULL //end of list, not found


or Temp-%26gt;bankId is True, they equal. And this is the node to delete right? But you don't check that.





Also, if temp ==head in the beginning, why have back check it in the else? else if(back == NULL) // Check to see if item is first in list


if(temp == NULL)


return false; // Not found so return false


else if(temp-%26gt;getbankID()==bankID)


DELETE HERE








Not sure, but thats how I see it
Reply:Need more information. There are many possibilities.





1) It could be the bankid is actually there so it removes the node and returns true.





2) It could be you're not maintaining the pointers in your list correctly, causing the program to iterate over nodes that you think are deleted. If bankID is found in those nodes, that would cause this function to return true.


No comments:

Post a Comment