•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,504 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,683 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3906 | Replies: 3
![]() |
•
•
Join Date: Aug 2004
Posts: 6
Reputation:
Rep Power: 0
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?
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!";•
•
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation:
Rep Power: 5
Solved Threads: 10
•
•
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation:
Rep Power: 5
Solved Threads: 10
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)
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- help implementing singly linked list (C++)
- adding and deleting node in a linked list (C++)
- remove method linked list (C)
- recursive linked list (C++)
- Linked List (C)
- Adding to linked list from external file (C)
Other Threads in the C++ Forum
- Previous Thread: Can Any One Help With This???
- Next Thread: looking for "real world" C++ code


Linear Mode