尝试实现一个函数以删除链表中的结构,同时将它前面的结构分配给它后面的结构



我知道有更好的方法来做我正在尝试的事情,但这是我用来学习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);
}

正在使用任意值来测试程序,我不需要将其完全实现为显然用于游戏的功能。任何帮助都非常感谢。

我希望以下是你想要的。从单向链表中删除元素。

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;

}

相关内容

  • 没有找到相关文章

最新更新