• Advertisement

Archived

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

linked list problem

This topic is 5489 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

My list isn't expanding for some reason I can't figure out. Here is the code I use to add a node to the list and the code I use to create a list.
    
void CEmitter::addNode( int nId )
{
	CParticle * pNew, * pCurr;
                                                                

	pNew = new CParticle( nId );
                                                                 
	if ( ! m_pHead ) // m_pHead = list head

	{
		m_pHead = pNew;
	}
	else
	{
		pCurr = m_pHead;

		while ( pCurr ) 
			pCurr = pCurr->next;

		pCurr = pNew;
	}
}
  

// Now on to creating the list


      
void CEmitter::InitEmitter( int n, float x, float y, float z )
{
        // set the origin

	m_x = x;
	m_y = y;
	m_z = z;
                                                                 
        // create a list n long 

	for ( int i = 0; i < n; i++ )
	{
		addNode( i );
	}
}
    
I try to make a list of 5 nodes and I only get one! Why is this, can some one please help me? [edited by - sakky on February 8, 2003 1:29:06 PM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by sakky
pCurr = m_pHead;
while ( pCurr )
pCurr = pCurr->next;
pCurr = pNew;



Unless pCurr is a member variable that is used in some strange way, I don''t see how that could work...try:


pCurr = m_pHead;
while ( pCurr->next )
pCurr = pCurr->next;
pCurr->next = pNew;


Oh and always make sure to initialize your "next" pointer to 0, lest the wrath of god fall upon you.



______________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________

Share this post


Link to post
Share on other sites
Ah, I see! You're setting the local variable pCurr to pNew. Needless to say, it doesn't get added to the list that way.

How about this?

pCurr = m_pHead;
while ( pCurr->next )
pCurr = pCurr->next;
pCurr->next = pNew;


[edited by - Kurioes on February 8, 2003 3:46:55 PM]

Share this post


Link to post
Share on other sites
Damn you guys are good. I guess I need to go back and study my linked-list again. I should have seen that dumb error before, and I thought I did. But I guess not huh? So does thismean I''m a bad programmer because I couldn''t see that one!? I is a pretty dumb logic error( on my part ).

Share this post


Link to post
Share on other sites
Nope, it doesn't mean you're a bad programmer... I make stupid mistakes like this all the time (I'm not claiming to be a good one). How about this one?


[edited by - Kurioes on February 8, 2003 3:46:09 PM]

Share this post


Link to post
Share on other sites
Owww, come on.... I spent almost 6 month trying to debug my first DDraw app, only to find out I had to open "Test.bmp" instead of "Test".

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more

Share this post


Link to post
Share on other sites

  • Advertisement