943,703 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6283
  • C++ RSS
Aug 9th, 2004
0

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

Expand Post »
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?
C++ Syntax (Toggle Plain Text)
  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!";
Reputation Points: 10
Solved Threads: 0
Newbie Poster
paynekiller is offline Offline
6 posts
since Aug 2004
Aug 9th, 2004
0

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

Because you don't update ptrHead. If you delete the last node, ptrHead should become NULL.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 10th, 2004
0

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

but ptrHead just points to the first node in the list, setting it to NULL would delete that node
Reputation Points: 10
Solved Threads: 0
Newbie Poster
paynekiller is offline Offline
6 posts
since Aug 2004
Aug 10th, 2004
0

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

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)
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can Any One Help With This???
Next Thread in C++ Forum Timeline: looking for "real world" C++ code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC