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
Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 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