Why doesn't this remove the last node in a linked list?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2004
Posts: 6
Reputation: paynekiller is an unknown quantity at this point 
Solved Threads: 0
paynekiller paynekiller is offline Offline
Newbie Poster

Why doesn't this remove the last node in a linked list?

 
0
  #1
Aug 9th, 2004
I've got a singly linked list, and the following code is supposed to delete all nodes that are ranked as "Trainee", it deletes them all, but if the last node is a "Trainee", it will not delete it, why is that?
  1. void LinkedList::removeLowest()
  2. {
  3. Soldier * ptrDelete;
  4. ptrCurrent = ptrHead;
  5. while(ptrCurrent != NULL)
  6. {
  7. if(!strcmp(ptrCurrent->getRank(), "Trainee"))
  8. {
  9. ptrCurrent->Show();
  10. ptrDelete = ptrCurrent;
  11. ptrCurrent = ptrCurrent->ptrNext;
  12. delete ptrDelete;
  13. }
  14. else
  15. {
  16. ptrCurrent = ptrCurrent->ptrNext;
  17. }
  18. }
  19.  
  20. cout << "\nTrainees Sacked!";
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Why doesn't this remove the last node in a linked list?

 
0
  #2
Aug 9th, 2004
Because you don't update ptrHead. If you delete the last node, ptrHead should become NULL.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 6
Reputation: paynekiller is an unknown quantity at this point 
Solved Threads: 0
paynekiller paynekiller is offline Offline
Newbie Poster

Re: Why doesn't this remove the last node in a linked list?

 
0
  #3
Aug 10th, 2004
but ptrHead just points to the first node in the list, setting it to NULL would delete that node
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Why doesn't this remove the last node in a linked list?

 
0
  #4
Aug 10th, 2004
Sorry, I misunderstood; I thought you meant Last as in "last remaining".

You have a list like this:

head-->first-->second-->third-->last

and you want to delete second because she is a trainee.

your code does this:

ptrCurrent points to second
show second
set ptrCurrent to point to third
delete second

Unless the destructor does some work on the entire list, you have this list:

head-->first-->[deleted second]-->third-->last

1) Does the destructor move first's next pointer to point to third (second's next)?

2) If you delete 'first', who updates the head pointer to point to first's next pointer?

3) If you delete 'last' does third get updated to point to null (last's next)? (note this would work fine if (1) works fine, I think)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC