Solved

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

Posted on 2004-03-20
2
232 Views
Last Modified: 2010-04-01
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);
}
0
Comment
Question by:Eifson
2 Comments
 
LVL 4

Accepted Solution

by:
ArcaArtem earned 125 total points
Comment Utility
Hello,

Here's the missing NULL-check:

    while(head != NULL && head->getFighters()->getiCurrentHullHealth() == 0 && head->getFighters()->getiCurrentShieldHealth() == 0)

HTH,
Arca
0
 

Author Comment

by:Eifson
Comment Utility
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;
      }
}
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now