[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-03-20
2
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
ArcaArtem earned 375 total points
ID: 10642791
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
ID: 10643111
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

649 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