| | |
Why doesn't this remove the last node in a linked list?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2004
Posts: 6
Reputation:
Solved Threads: 0
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)
void LinkedList::removeLowest() { Soldier * ptrDelete; ptrCurrent = ptrHead; while(ptrCurrent != NULL) { if(!strcmp(ptrCurrent->getRank(), "Trainee")) { ptrCurrent->Show(); ptrDelete = ptrCurrent; ptrCurrent = ptrCurrent->ptrNext; delete ptrDelete; } else { ptrCurrent = ptrCurrent->ptrNext; } } cout << "\nTrainees Sacked!";
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)
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)
![]() |
Similar Threads
- Choosing a Node in a Linked List (C++)
- Adding a node to my linked list and printing it makes my program crash (C++)
- multiple stack in one dimentional array using node/ linked list (Java)
- How to remove a node from link list (Java)
- how to remove node from link list (Java)
- stack , queue and linked list (C++)
- remove method linked list (C)
Other Threads in the C++ Forum
- Previous Thread: Can Any One Help With This???
- Next Thread: looking for "real world" C++ code
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





