0

Here is the whole project, in a zip file. I'm having trouble getting the delete function to work. I can delete head just fine, but when I try to delete either something at the end, or something in the middle, it does not work out. I'm sure it has something to do with my logic being off, and what i'm showing you is a recent edit, so it might be worse than what it was.

template <class LE> 
void List<LE>::remove ()  // Remove element
{
	ListNode<LE> *temp = head;
	if (!empty())
	{	
		if(cursor == head) //case 1 corpse = head
		{
			head = head -> next;
			delete temp;
		}
		else if(cursor -> next == NULL) //case 2 corpse is at the end
		{
			while (temp -> next -> next != NULL)
				temp = temp -> next;
			temp = temp -> next;
			temp -> next = NULL;
			delete cursor;
			delete temp;
			cursor = head;
		}
		else //case 3 corpse is in middle somewhere
		{
			while (temp -> next != cursor)
				temp = temp -> next;
			temp -> next = cursor -> next;
			delete cursor;
			delete temp;
			cursor = head;
		}
	}
}
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Avaviel
0

I'm using unix, so I can't look at your source files,
but theres something I don't understand in you code for case2

Your are setting temp->next = null;
which probly mean that you want temp->next->next to be deleted instead of temp.

But your should do it before you set the next to null
something like

temp = temp -> next;
delete temp->next;
temp -> next = NULL;
delete cursor;

I have no idea what your 'curser' is though,
so I might be way of.
I haven't looked at your case3.

0

Thanks for the help, but I've talked to the professor and he cleared it up!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.