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;
		}
	}
}

Recommended Answers

All 2 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.