Solved

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

Posted on 2004-03-20
2
236 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 125 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

  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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

733 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