User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Aug 2004
Posts: 6
Reputation: paynekiller is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
paynekiller paynekiller is offline Offline
Newbie Poster

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

  #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?
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!";
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

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

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

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

  #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  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC