Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

randomDecay

assigning one linked list -> another

This topic is 6162 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

here is the code for my assignment = overload to assign one list to another. Well all I have so far is the deletion of the first list. Then I will make a new list based off the second. But I get a nasty debug error during run-time that I can''t figure out.
  
// Overloaded functions

List &List::operator=( const List &p_list )
{
	ListNode *pTemp = m_pHead;

	// delete the first list

	while ( pTemp != NULL )
	{		
		Delete( pTemp->GetData() );
							  
		pTemp = pTemp->GetNext();
	}

	return ( *this );
}
  
Any help would be ...er.. helpful.

Share this post


Link to post
Share on other sites
Advertisement
If you wanna get advanced, look into reference counting. If you aren't doing this as a learning exercise then look into the STL linked list implementation, it'll save you alot of time.

Oh yeah, you really shouldn't be deleting the first list on the assignment operator - you should copy it if you are not going to reference count it. This is because users of your class will expect the data still to be in the first list... look at this:

        

List list1, list2;

list1.insert(1, "some data"); // insert data at index 1


list2 = list1; // assignment operator called, list1 is deleted


list1.getdata(1); // if i was using your class i'ld still expect "some data" to be in list1 (at index 1),

// else i'ld be well annoyed





[edit] Re-reading your post im not sure if this is what you meant to do... what exactly do you mean by "first" and "second" list?

Edited by - Spiral on February 3, 2002 4:53:17 AM

Share this post


Link to post
Share on other sites
ex......

List list1;
List list2;

list1.Add( 5 );
list1.Add( 1 );
list1.Add( 10 );

list2.Add( 7 );
list2.Add( 16 );

list1 = list2;

list1.Display();

// should display the following
7
16

see?

Share this post


Link to post
Share on other sites
Got it. Now what _exactly_ does Delete() do in your first post? Thats most likely causing the trouble.

Share this post


Link to post
Share on other sites
//delete
  
oid List::Delete( int p_data )
{
bool bDone = false;
ListNode *pCurrent = m_pHead;

if ( pCurrent->GetData() == p_data )
{
m_pHead = pCurrent->GetNext();
delete pCurrent;
bDone = true;
}

while ( pCurrent->GetNext() != NULL && bDone == false )
{
ListNode *pTemp = pCurrent->GetNext();

if ( pTemp->GetData() == p_data )
{
pCurrent->SetNext( pTemp->GetNext() );
delete pTemp;
bDone = true;
}
else
{
pCurrent = pCurrent->GetNext();
}
}
}

Share this post


Link to post
Share on other sites
Well... looking at your original piece of code:

      
Delete( pTemp->GetData() );

pTemp = pTemp->GetNext();


If im following Delete() correctly that means pTemp wont exist anymore once the function returns. And then you to call ->GetNext() upon it would cause various nasty things to happen... you'll have to use a temporary ListNode and alternate between them.

Like thus:

  

// Overloaded functions

List &List::operator=( const List &p_list )
{
ListNode *pTemp = m_pHead, *pTemp2 = NULL;

// delete the first list

while ( true )
{
if (pTemp) {
pTemp2 = pTemp->GetNext();
Delete(pTemp);
pTemp = NULL;
}
else if (pTemp2) {
pTemp = pTemp2->GetNext();
Delete(pTemp2);
pTemp2 = NULL;
}
else {
break;
}
}

return ( *this );
}



Probably not the best way to go about it, but it should work. I havent tested it or anything.

Edited by - Spiral on February 3, 2002 7:05:31 AM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!