0

私が試みていることを行うためのより良い方法があることは理解していますが、これは私が C++ を学ぶために使用している本の質問であり、先に進む前にいくつかの基本を理解するのに役立ちます。とにかく、ここに私のコードがあります:

#include <iostream>

using namespace std;

struct EnemySpaceShip
{
    int weapon_power;
    int xcoord;
    int ycoord;
    EnemySpaceShip *nextEnemy;
};

EnemySpaceShip* getNewEnemy(EnemySpaceShip* p_enemies) // Creates a new EnemySpaceShip in linked list p_enemies
{
    EnemySpaceShip *p_ship = new EnemySpaceShip;
    p_ship->xcoord = 0;
    p_ship->ycoord = 0;
    p_ship->weapon_power = 10;
    p_ship->nextEnemy = p_enemies;
    p_enemies = p_ship;
    return p_ship;
}

EnemySpaceShip* findPreRemove(EnemySpaceShip* p_enemies, int x_attack, int y_attack) // finds the element that is before the ship to be removed or returns NULL
{
    EnemySpaceShip *p_current = p_enemies;
    EnemySpaceShip *initialShip = p_enemies;
    int i= 0;
    while (p_current != NULL)
    {
        i++;
        if (p_current->xcoord == x_attack && p_current->ycoord == y_attack)
        {
            if (i == 1)
            {
                delete initialShip;
                delete p_current;
                return NULL;
            }
            else
            {
                for (int j = 1; j < i - 1; j++)
                {
                    initialShip = initialShip->nextEnemy;
                }
                delete p_current;
                return initialShip;
            }
        }
        p_current = p_current->nextEnemy;
    }
    return NULL;
}

EnemySpaceShip* findRemove(EnemySpaceShip* p_enemies, int x_attack, int y_attack)
{
    EnemySpaceShip *p_current = p_enemies;
    while (p_current != NULL)
    {
        if (p_current->xcoord == x_attack && p_current->ycoord == y_attack)
        {
            return p_current;
        }
        p_current = p_current->nextEnemy;
    }
}

EnemySpaceShip* removeEnemyShip(EnemySpaceShip *p_ship) // deletes the ship parameter and returns the ship after it in the list
{
    EnemySpaceShip *enemyAfterRemove = new EnemySpaceShip;
    enemyAfterRemove = p_ship->nextEnemy;
    delete p_ship;
    return enemyAfterRemove;
}

int main()
{
    EnemySpaceShip *p_enemies = NULL;
    EnemySpaceShip *Ship1 = getNewEnemy(p_enemies);
    EnemySpaceShip *Ship2 = getNewEnemy(p_enemies);
    EnemySpaceShip *Ship3 = getNewEnemy(p_enemies);

    Ship3->xcoord = 5; //arbitrary numbers to test the code
    Ship3->ycoord = 5;

    EnemySpaceShip *ShipBeforeRemove = findPreRemove(p_enemies, 5, 5);
    EnemySpaceShip *ShipToRemove = findRemove(p_enemies, 5, 5);
    ShipBeforeRemove->nextEnemy = removeEnemyShip(ShipToRemove);
}

プログラムをテストするために任意の値を使用しています。明らかに使用されているゲームの機能としてこれを完全に実装する必要はありません。どんな助けでも大歓迎です。

4

1 に答える 1

0

以下があなたが望むものであることを願っています.単一リンクリストから要素を削除する..

EnemySpaceShip* removeFromList(EnemySpaceShip* p_enemies, int x_attack, int y_attack)
{
    if(p_enemies == NULL)
        return NULL;

    EnemySpaceShip *p1 = p_enemies;
    EnemySpaceShip *p2 = p1->next;

    if(p2 == NULL)
    {
        // Trivial case..

        if(p1->xcoord == x_attack && p1->ycoord == y_attack)
        {
            delete p_enemies;
            p_enemies = NULL;
        }

        return NULL;
    }

    while(p2 != NULL)
    {
         if(p2->xcoord == x_attack && p2->ycoord == y_attack)
         {
             // Element found, remove it and assign its previous pointer
             // as the next pointer of the deleted one..
             p1->next = p2->next;
             delete p2;
             return p1;
         }
         else
         {
             p1 = p2;
             p2 = p2->next;
         }
    }

    return NULL;

}

于 2012-05-11T19:27:05.100 に答える