Advertisement

Need help with doubly linked lists

Started by March 02, 2002 09:09 AM
10 comments, last by NerdJNerdBird 22 years, 11 months ago

  void updatebullets(){  bool deleteprev = false;  for(curpoint=head;curpoint!=NULL;curpoint=curpoint->next){    //modify bullets    // this next section can leave you with curpoint being    // an invalid pointer after deletebullet() is called    if (deleteprev){deletebullet(curpoint->prev);deleteprev = false;    }// so when you get down to here and you check curpoint->next// the program will crash    if ((x < -SCREEN_X/2) || (x > SCREEN_X/2) ||(y > SCREEN_Y/2) || (y < -SCREEN_Y/2)){if (curpoint->next != NULL)    deleteprev = true;else    deletebullet(curpoint);    }  }}// An alternative method would be something like thisvoid updatebullets(){    bullet * curpoint = head;    while(curpoint)    {        if ((curpoint->x < -1/2) || (curpoint->x > 1/2) || (curpoint->y > 1/2) || (curpoint->y < -1/2))        {            // a temp copy of curpoint''s next item            bullet *temp = curpoint->next;            deletebullet(curpoint);            // after deleting curpoint set curpoint            // to the next item.  either a real node            // or 0            curpoint = temp;        }        else            curpoint = curpoint->next;    }}  
You are my hero. I love you, thank you for fixing that. It confoosed me out of my mind.

This topic is closed to new replies.

Advertisement