Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Double ended link list help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 MagicalBearClaw   Members   -  Reputation: 356

Like
0Likes
Like

Posted 13 January 2013 - 01:55 PM

Hi I'm currently working on a double ended link list and currently testing it as I implement it.

 

There are three  parts of code I'm Fuzzy on

 

First is the copy constructor and second is the assignment operator. the third is the implementation of the destroy  member function(deleting the list)

 

Currently this is the implantation I have for the assignment operator.

 

LinkedList& LinkedList::operator=(const LinkedList& rhs)
{
	if(this == &rhs)
		return *this;

	this->Destroy();

	this->mFirst = rhs.mFirst;
	this->mLast = rhs.mLast;


	return *this;
}

 

As for the copy constructor, I am not sure how to implement it .

 

then the destroy member function

 

void LinkedList::Destroy() 
{
	LinkedNode* current = nullptr;	
	if(!IsEmpty())
	{
		current =  mLast->previous;
		while(current != nullptr)
		{
			delete mLast;
			current->next = nullptr;
			mLast = current;
			current = mLast->previous;
		}
		delete mFirst;
		mLast = nullptr;
		mFirst = nullptr;
	}
}

Here is my Main function

 

int main()
{
	LinkedList list;
	list.InsertFirst(5);
	list.InsertFirst(24);
	list.InsertFirst(32);

	LinkedList list2;

	list2.InsertFirst(1);
	list2.InsertFirst(2);
	list2.InsertFirst(3);
	list2.InsertFirst(4);
	list2.InsertFirst(5);


	LinkedNode* current;

	current =  list.GetFirst();

	while (current != nullptr)
	{
		cout << current->data << endl;
		
		current = current->next;
	}

	cout << endl;

	list = list2;

	current =  list.GetFirst();

	while (current != nullptr)
	{
		cout << current->data << endl;
		
		current = current->next;
	}
	cout << endl;
}

 

now when executing this I get an error: debug assertion failed expression _block_type_is_valid(pHead->nBlockUse)

 

I'm unsure what this is error is I tried debugging for a while but not sure where things are going wrong. I believe it has something to do with deleting invalid memory.

 

i know it has something to do with the destroy  member function and copy assignment operator, as if i remove list = list2 from the main function everything works fine.

 

 

I appreciate any input/help on this

 

 

 

 

 



Sponsor:

#2 rip-off   Moderators   -  Reputation: 8518

Like
1Likes
Like

Posted 13 January 2013 - 02:13 PM

You need to do a "deep copy" in your assignment operator. If we ignore exception safety for the moment, the thing to do is to iterate through the objects in the "rhs" list, and insert them into the list being assigned to.

 

What you end up with your current implementation is two lists that point to the same set of nodes. When one of these lists is destroyed, the other one is corrupted because it's pointers point at de-allocated memory (or worse, previously de-allocated memory that is now being re-used elsewhere).

 

I'd probably implement destroy as a simple forward loop, starting at the head and deleting each node:

 
 
</p><div>LinkedNode* current = mFirst;</div>
<div>while(current)</div>
<div>{</div>
<div>    LinkedNode *temp = current;</div>
<div>    current = current->next;</div>
<div>    delete temp;</div>
<div>}</div>
<div>mFirst = nullptr;</div>
<div>mLast = nullptr;</div>

Your code might be correct, but I'm finding it much harder to reason about.



#3 ScottLowe   Members   -  Reputation: 103

Like
0Likes
Like

Posted 13 January 2013 - 03:55 PM

I wanted to make a suggestion that might make things a bit easier. Have you tried just using a vector with a wrapper class to allow for easy traversing? Bjarne Stroustrup in his C++11 keynote speech mentioned that due to caching, vectors are actually much faster than linked lists, and this would be very pertinent for game development. As the video of his speech was over an hour long, check out this post: http://bulldozer00.com/2012/02/09/vectors-and-lists/ . It includes Bjarne's slides with graphs of benchmarks.



#4 MagicalBearClaw   Members   -  Reputation: 356

Like
0Likes
Like

Posted 13 January 2013 - 06:30 PM

rip-off thanks for pointing me in the right direction, 

 

OK so after of bit of trial and error I have come up with this solution.

 

I wanted to know if this is valid? I mean it works with no errors. but then I am not sure if I am leaking memory or doing something im not supposed to.

 

thanks again for the help guys

 

LinkedList& LinkedList::operator=(const LinkedList& rhs)
{
	if(this == &rhs)
		return *this;

	this->Destroy();
	
	LinkedNode* rhsNode = nullptr;
	LinkedNode* node = nullptr;
	LinkedNode* temp =  nullptr;

	rhsNode = rhs.mFirst;

	while(rhsNode != nullptr)
	{
		node = new LinkedNode;
		if(IsEmpty())
		{
			mFirst = node;
			mLast =  node;
			node->data = rhsNode->data;

			mFirst->previous =  nullptr;
			mLast->next =  nullptr;
		}
		else
		{

			temp = mLast;
			mLast = node;
			temp-> next =  mLast;
			mLast->previous = temp;
			mLast->data = rhsNode->data;
		}

		rhsNode =rhsNode->next;
	}

	mLast->next = nullptr;

	rhsNode =nullptr;
	node = nullptr;
	temp = nullptr;

	return *this;
}

Scottlowe, thanks for your interesting link. The reason I am building my own double ended linked list is to use this as an exercise. I don't plan to use these data structure projects in any project but to learn. 



#5 SiCrane   Moderators   -  Reputation: 9628

Like
2Likes
Like

Posted 13 January 2013 - 08:24 PM

You might find it easier to reason about your programs if you broke them into smaller chunks. For example, let's say you had two other functions: PushBack(), which adds a new node to the end of the linked list with the given data and PopBack() which removes the node at the end of the linked list. These are pretty useful functions to have for a linked list in and of themselves, but you could use them to implement things like copying another linked list (by calling PushBack() for the data you see in the other list) and clearing your linked list (by repeatedly calling PopBack() while the list isn't empty).

#6 MagicalBearClaw   Members   -  Reputation: 356

Like
0Likes
Like

Posted 13 January 2013 - 08:38 PM

Thanks SiCrane, I have not implemented any kind of remove functions yet, only insertions. Although I do see how the remove functions could make my Destroy Function more clean and simple. I really appreciate the help you guys have given me ;).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS