For some reason my function to delete a node from the middle of a list is deleting the first node instead of the one specified by the user. Here’s my function:
template< class NODETYPE >
bool List< NODETYPE >::removeMiddle( NODETYPE &value, int i )
{
ListNode <NODETYPE> * tempPtr = firstPtr;
int counter=1;
if ( isEmpty() )
return false;
if (i <= 0)
return false;
while (tempPtr != 0 && counter < i){
counter++;
if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else
firstPtr = firstPtr->nextPtr;
if (counter == i){
value = tempPtr->data; // data being removed
delete tempPtr;
}
}
return true;
RecordCounter--;
}
Can anyone help point me in the right direction?
I’m assuming it’s because this:
should check and modify
tempPtr, notfirstPtr.Since you don’t check by
tempPtr, your function callsbut
tempPtrwas initially set tofirstPtrand never modified afterwards.Of course, you could have easily spotted this by debugging a bit, so I won’t correct the code for you, but this is a good starting point.