Hmm... I tried the code myself and I guess I was wrong abut the modifications to the existing insert function.
I wound up writing the new insert function myself (and it's working). It's actually simpler than the given version.
There are only 2 insertion scenarios
1) Either first==0 (empty list) or prev==0 (which means to insert before first).
Either way you are adding a new first Node, so:
1 2
|
newNode->next = first;
first = newNode;
|
2) else (only other case) you are inserting after prev, in which case:
1 2
|
newNode->next = prev->next;
prev->next = newNode;
|
No for loop needed!
Re. the erase() function. Really dude? You thought that might work?
You need to write this one from scratch. There is no other given version to work from.
I have my version working.
Again, there are 2 erasure scenarios:
1) first->payload == value. Erase the 1st Node. I'll give you the code for this:
1 2 3
|
Node* badNode = first;// Node to be deleted
first = first->next;// new first Node is next in list.
delete badNode;
|
2) else (only other case) value is deeper in the list. Consider your profs. comments re. "sneaking up" on the Node to be erased. You are seeking the Node previous to the one to be erased, so you can re assign its next pointer to badNode->next. Admittedly, this part is a little tricky.
Some use 2 local Node*'s prev and curr, You iterate until curr->payload == value, and keep prev updated so it's always pointing one behind curr.
I think my approach is cleaner. Having examined first->payload explicitly, we can start at
Node* curr = first;
and confidently watch for curr->next->payload == value while iterating through the list(using curr). Then:
1 2 3
|
Node* badNode = curr->next;// save pointer to node to be removed
curr->next = badNode->next;// link across badNode
if( badNode ) delete badNode;
|
Try to work off of that. Post back with your new functions after giving it a serious shot.
And, watch the content of your PMs. That was risky for you.