尝试实现一个函数来删除链表中的结构,同时在它之前指定结构指向它之后的结构

时间:2012-05-11 19:05:11

标签: c++ struct linked-list

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

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

1 个答案:

答案 0 :(得分: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;

}