Link to home
Start Free TrialLog in
Avatar of Eifson
Eifson

asked on

My function deleteDestroyedShips() not quite working, please help.

Hello,

I am writing a game and have writen a function for my linked list that contains my fighter ship objects.  The function I am having problems with is the deleteDestroyedShips() function.  It is supposed to go through the linked list and delete all of the ships whose shields have 0 hit points and whose hulls have 0 hit points.  So far it works, in 3 out of my 4 test cases for the functions.

1)If I have 1 ship in the list and its hull and shield are zero it deletes it and sets the nodecounter for that linked list to zero
2)If I have 2 ships in the list and 1 of the ships hull and shield are zero it deletes that ship and sets the node counter to 1 and still has the other ship in the list
3)If I have 3 ships in the list and no matter which 2 ships have their hull and shields at zero it will delete the appropriate ones and set the list correctly.
4)If I have 4 ships and I set every one of their shields and hull hit points to zero, it errors out, it does not like it if I want to delete all the ships if there are more than one.

However, if there is only one ship it deletes it fine.  I have a feeling that I am missing a check for pointer pointing to NULL somewhere.... below is the function.
In the function you will see getFighters().
here is what that does: Fighters *getFighters(){return fighter;}

void FighterLinkedList::deleteDestroyedShips()
{
     if(head == NULL)
     {
          cout << "Linked list is empty" << endl;
          return;
     }

     if(head->getLink() == NULL && head->getFighters()->getiCurrentHullHealth() == 0 && head->getFighters()->getiCurrentShieldHealth() == 0)
     {
          delete head->getFighters();
          delete head;
          head = NULL;
          nodeCount--;
          return;
     }
     FighterNode *ptr1, *ptr2;

     while(head->getFighters()->getiCurrentHullHealth() == 0 && head->getFighters()->getiCurrentShieldHealth() == 0)
     {
          ptr1 = head;
          ptr2 = head->getLink();
          head = ptr2;
          delete ptr1->getFighters();
          delete ptr1;
          nodeCount--;
     }

     ptr1 = head;
     ptr2 = head->getLink();

     do
     {
           if(ptr2->getFighters()->getiCurrentHullHealth() == 0 && ptr2->getFighters()->getiCurrentShieldHealth() == 0)
          {
               ptr1->setLink(ptr2->getLink());
               delete ptr2->getFighters();
               delete ptr2;
               ptr2 = ptr1->getLink();
               nodeCount--;
          }
          else
          {
               ptr1 = ptr2;
               ptr2 = ptr2->getLink();
          }
     }while(ptr2 != NULL);
}
ASKER CERTIFIED SOLUTION
Avatar of ArcaArtem
ArcaArtem

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eifson
Eifson

ASKER

ArcaArtem,

Thank you for your response.  You got me on the right track, I winded up doing the following to fix my problem.  Below is the new code in the function.

void FighterLinkedList::deleteDestroyedShips()
{
      if(head == NULL)
      {
            cout << "Linked list is empty" << endl;
            return;
      }

      if(head->getLink() == NULL && head->getFighters()->getiCurrentHullHealth() == 0 && head->getFighters()->getiCurrentShieldHealth() == 0)
      {
            cout << head->getFighters()->getiHullType() << endl;
            delete head->getFighters();
            delete head;
            head = NULL;
            nodeCount--;
            return;
      }

      FighterNode *ptr1, *ptr2;

      while(head->getLink() != NULL && head->getFighters()->getiCurrentHullHealth() == 0 && head->getFighters()->getiCurrentShieldHealth() == 0)
      {
            ptr1 = head;
            ptr2 = head->getLink();
            head = ptr2;
            delete ptr1->getFighters();
            delete ptr1;
            nodeCount--;
      }

      ptr1 = head;
      ptr2 = head->getLink();

      while(ptr2 != NULL)
      {
            if(ptr2->getFighters()->getiCurrentHullHealth() == 0 && ptr2->getFighters()->getiCurrentShieldHealth() == 0)
            {
                  ptr1->setLink(ptr2->getLink());
                  delete ptr2->getFighters();
                  delete ptr2;
                  ptr2 = ptr1->getLink();
                  nodeCount--;
            }
            else
            {
                  ptr1 = ptr2;
                  ptr2 = ptr2->getLink();
            }
      }

      if(head->getLink() == NULL && head->getFighters()->getiCurrentHullHealth() == 0 && head->getFighters()->getiCurrentShieldHealth() == 0)
      {
            delete head->getFighters();
            delete head;
            head = NULL;
            nodeCount--;
            return;
      }
}