Advertisement

I would appreciate advice on pointers for particle stuff please

Started by March 11, 2002 02:41 PM
2 comments, last by csxpcm 22 years, 11 months ago
Hello all, I have a pointer question im trying to figure out, i think i understand the concept of pointers to manipulate a linked list but I would really appreciate someone just checking through what i think is correct and commenting on any of it, in the example below, am i right in saying the following: Im trying to join the new particle, that was just pulled off pFree and put it on the pUse list. I read that the "&" operator is used to find the address for a particular variable. So, at ** 1 ** the n->prev is set to the beginning address of pUse. So at this point of the program, is ''n'' set to the memory address of the first element in pUse? therefore, since the original first element isnt changed yet, we have two links in pUse sharing the same beginning memory address? am i correct up to now? then in ** 2 ** , I set n->next. I make this point to the element that was originally at the front of the pUse list. Now its the next one along? so therefore (at stage 2 of the program), I end up with the new element that has now been added having ''prev'' set to the memory address the whole list (i.e. the addresss of itself (the first element) and next pointing to the second element, which was originally the first. Is this right? Is this correct? could someone tell me if im wrong Thanks in advance!! Any suggestions much appreciated! CParticle* CParticleSystemartNew(void) { if (!pFree) return 0; CParticle *n = pFree; pFree = pFree->next; n->prev = &pUse **** 1 **** n->next = pUse.next; **** 2 **** pUse.next->prev = n; pUse.next = n; return n; } ############################################################# class CParticle { public: CParticle(); virtual ~CParticle(); CVector pos; CVector vel; CParticle *next, *prev; }; ############################################################# class CParticleSystem { public: CParticleSystem(); // constructor virtual ~CParticleSystem(); // destructor // and other stuff protected: CParticle *pAll; // single link CParticle pUse; // double link CParticle *pFree; // single link CParticleSystem *next, *prev; };
The pUse node itself is a dummy node that is never used. It is there so when you delete items from the linked list you don''t have to do checks for special cases at the end of the list. You also use the address of pUse when looping through particles to see that you have made a complete run through all particles.

In #1, you set the address to the pUse node itself. There is no concept of beginning or end here. The first element in the pUse list is pUse.next.

In #2, you are setting the next node to the first element in the pUse list.

You should probably e-mail the author of the code you are copying so you can get some clarification about what they are doing.
Advertisement
THANKS for the previous advice!!!
erm, but im not sure if i fully understand it yet.
So, from what has been previously said, do we end up with a long list of pAll (used Particles) which terminates with a single pUse (dummy node) to denote the end? is this why we link in a dummy node at the end?

So, if pUse is just a dummy node that is never used, whats he point of the next 2 lines?

pUse.next->prev = n;
pUse.next = n;

what are these attibutes set for then? Not sure why "pUse" has to be defined as a double link.

I cant rememeber where the code came from so cant email the respective author, so was hoping someone might be able to explain this section, cos im really stuck in understanding it.

Any suggestions are much appreciated!
Thank-you!! in Advance!!
pUse is not at the end, it is at the start, but the last node in the list has its next pointer set to pUse. The sole purpose of the doubly linked list with a dummy head node is to make deleting from the list easier. Without double links you would have to walk the entire list to delete a node. Without the dummy node you would have to write special checks when deleting a node.

The two lines of code you pasted insert a node into the list. pUse.next is the first node in the list, but it can also be equal to pUse if the list is empty. pUse is setup initially to have its next and previous pointers point to itself, so it just forms a loop.

You should probably draw this out on paper or write your own code so you actually know what is going on.

This topic is closed to new replies.

Advertisement