Page 1 of 2

What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 3:52 pm
by xiphirx

Code: Select all


LinkedList::~LinkedList()
{
	while (first != 0) 
	{
		NodePointer ptr = first;
		first = ptr->next;
		delete ptr;
    }
	if (first == 0) cout << "List destroyed\n";
	else cout << "List not destroyed\n";
}


I always get a "Debug Assertion Failed!"
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

the code that is being executed

Code: Select all

   {
      LinkedList anotherList;
      for (int i = 0; i < 5; i++)
         anotherList.insert(i, 20 * i);
      cout << "\nHere's another list:\n" << anotherList << endl;
      cout << "Now destroying this list\n";
   }
   cout << "*** If the destructor was called, anotherList was destroyed ***\n";

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 3:59 pm
by Milch
Do you set everytime you create a new object the pointer to the next element to NULL?
And by the way, start writing NULL instead of "0" for pointer comparisons ;)

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:02 pm
by xiphirx
Milch wrote:Do you set everytime you create a new object the pointer to the next element to NULL?
And by the way, start writing NULL instead of "0" for pointer comparisons ;)
Yes, whenever a node is created, its "next" is set to "NULL".
I usually use NULL, but I was experimenting with everything I can think of lol. tbh I don't see why this doesn't work as I followed a worksheet exactly... and the worksheet does spoon feed you.

:/

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:03 pm
by avansc
pastebin the code in entirety and post the link.

edit: also, if you wanna use 0 that is perfectly fine. its the same thing. #define NULL (void*)0

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:10 pm
by xiphirx

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:13 pm
by avansc
k, give me 5 min to check it out.

edit: btw, i think you are deleting a null pointer, infact im pretty sure.

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:22 pm
by xiphirx
avansc wrote:
k, give me 5 min to check it out.

edit: btw, i think you are deleting a null pointer, infact im pretty sure.
That is what I think I am doing too, but I don't know why its trying to delete one...

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:28 pm
by avansc
xiphirx wrote:
avansc wrote:
k, give me 5 min to check it out.

edit: btw, i think you are deleting a null pointer, infact im pretty sure.
That is what I think I am doing too, but I don't know why its trying to delete one...
mmm, im not sure what happening, i'll have to look at it for a min longer.

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:29 pm
by Milch
I'm not sure - but where's the Constructor for LinkedList?
Maybe you didnt set first to NULL?

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:30 pm
by avansc
Milch wrote:I'm not sure - but where's the Constructor for LinkedList?
Maybe you didnt set first to NULL?
no he has not destructor for his node, thats the problem

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:32 pm
by xiphirx
Milch wrote:I'm not sure - but where's the Constructor for LinkedList?
Maybe you didnt set first to NULL?

Code: Select all

LinkedList(){first = NULL; mySize = 0;}
its in there
no he has not destructor for his node, thats the problem
So I have to make a destructor for my node class? (is wondering why it wasn't mention in this worksheet :/)

EDIT: just in case, this is what my worksheet tells me to do...
1. Declare a NodePointer pointer ptr and set it to point to the first node in the list
2. Traverse the list and for each node do the following
a. set first to the next node after that pointed to by ptr (i.e. first = ptr->next)
b. use delete to deallocate the node pointed to by ptr (i.e. delete ptr)
c. set ptr equal to first

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:37 pm
by avansc
xiphirx wrote:
Milch wrote:I'm not sure - but where's the Constructor for LinkedList?
Maybe you didnt set first to NULL?

Code: Select all

LinkedList(){first = NULL; mySize = 0;}
its in there
no he has not destructor for his node, thats the problem
So I have to make a destructor for my node class? (is wondering why it wasn't mention in this worksheet :/)
well i dont know where you got that worksheet. but its one of the worse implementations i have ever seen of a linked list.
are you allowed to implement your own? id be willing to help you if you are interested.

edit: this Larry R. Nyhoff, looks like an accomplished author. you sure you followed that thing right if it is infact the same Larry R. Nyhoff.

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:40 pm
by xiphirx
avansc wrote:
xiphirx wrote:
Milch wrote:I'm not sure - but where's the Constructor for LinkedList?
Maybe you didnt set first to NULL?

Code: Select all

LinkedList(){first = NULL; mySize = 0;}
its in there
no he has not destructor for his node, thats the problem
So I have to make a destructor for my node class? (is wondering why it wasn't mention in this worksheet :/)
well i dont know where you got that worksheet. but its one of the worse implementations i have ever seen of a linked list.
are you allowed to implement your own? id be willing to help you if you are interested.
I'm supposed to follow it :( but I can add a node destructor I think... also, read my edited post above for my instructions :s

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:50 pm
by avansc
well you are missing the last step in the instructions
also you have the NodePointer in the wrong place.

Code: Select all

LinkedList::~LinkedList()
{
	NodePointer ptr = first;
	while (first->next) 
	{
		first = ptr->next;
		delete ptr;
		ptr = first;
    }
	if (first == 0) cout << "List destroyed\n";
	else cout << "List not destroyed\n";
}

Re: What is wrong with this Linked List destructor?

Posted: Mon Mar 29, 2010 4:55 pm
by xiphirx
avansc wrote:well you are missing the last step in the instructions
also you have the NodePointer in the wrong place.

Code: Select all

LinkedList::~LinkedList()
{
	NodePointer ptr = first;
	while (first->next) 
	{
		first = ptr->next;
		delete ptr;
		ptr = first;
    }
	if (first == 0) cout << "List destroyed\n";
	else cout << "List not destroyed\n";
}

This is what I had before I started experimenting, and it does not work :(