How to remove an element form a linked list

Hi, I am trying to learn c++ by my own using the Jumping into c++ book. So, im at the linked list part and I am trying to write a program which add and remove element from a linked list.

So, my add an element function work perfectly but there is a problem with my delete function. When I delete the first element of the list two times in a row the program crash and I dunno why.

Thanks for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 #include <iostream>
#include <string>

using namespace std;

struct EnemySpaceShip
{
    string ship_name;
    int x_coordinate;
    int y_coordinate;
    int weapon_power;
    EnemySpaceShip* p_next_enemy;
    EnemySpaceShip* p_last_enemy;
};

EnemySpaceShip* addNewEnemyToList(EnemySpaceShip* p_list, string name)
{
    EnemySpaceShip* p_ship = new EnemySpaceShip;
    p_ship->x_coordinate = 0;
    p_ship->y_coordinate = 0;
    p_ship->weapon_power = 20;
    p_ship->ship_name = name;
    p_ship->p_next_enemy = p_list;
    p_ship->p_last_enemy = NULL;
    return p_ship;
}

void display_list(EnemySpaceShip *p_list)
{
    EnemySpaceShip *p_current = p_list;
    while(p_current!=NULL)
    {
        cout << p_current->ship_name << " ";
        p_current = p_current->p_next_enemy;
    }
    cout << "\n";
}

EnemySpaceShip *delete_spaceship(string name, EnemySpaceShip* p_list)
{
    EnemySpaceShip *p_current = p_list;
    while(p_current!=NULL)
    {
        if(p_current->ship_name == name)
            break;
        else
            p_current = p_current->p_next_enemy;
    }
    if(p_current==NULL)
        cout << "The ship you want to be deleted doesn't exist" << endl;
    else
    {
        if(p_current->p_last_enemy==NULL)
        {
        p_current->p_next_enemy->p_last_enemy = NULL;
        delete p_current;
        }
        else
        {
        p_current->p_last_enemy->p_next_enemy = p_current->p_next_enemy;
        if(p_current->p_next_enemy!=NULL)
        p_current->p_next_enemy->p_last_enemy = p_current->p_last_enemy;
      delete p_current;
        }
    }
}

int main()
{
    EnemySpaceShip* p_enemies = NULL;

    string name;
    cout << "What is the name of the enemy ship?" << endl;
    cin >> name;

    p_enemies = addNewEnemyToList(p_enemies,name);

    display_list(p_enemies);

    string yesorno;

    while(1)
    {
        cout << "Do you wish to add another enemy?" << endl;
        cin >> yesorno;
        if(yesorno=="no")
            break;
        else
        {
            cout << "What is the name of the new enemy space ship?" << endl;
            cin >> name;
            p_enemies = addNewEnemyToList(p_enemies,name);
            p_enemies->p_next_enemy->p_last_enemy = p_enemies;
            display_list(p_enemies);
        }
    }

    while(1)
    {
        cout << "Do yo wish to delete an enemy space ship?" << endl;
        cin >> yesorno;
        if(yesorno=="no")
            break;
        else
        {
            cout << "What is the name of the enemy space ship you want to be deleted?" << endl;
            cin >> name;
             delete_spaceship(name,p_enemies);
            display_list(p_enemies);
        }
    }
    return 0;
}
I think that you are wrong saying that your add element function works perfectly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
EnemySpaceShip* addNewEnemyToList(EnemySpaceShip* p_list, string name)
{
    EnemySpaceShip* p_ship = new EnemySpaceShip;
    p_ship->x_coordinate = 0;
    p_ship->y_coordinate = 0;
    p_ship->weapon_power = 20;
    p_ship->ship_name = name;
    p_ship->p_next_enemy = p_list;
    p_ship->p_last_enemy = NULL;
    return p_ship;
}

What does the bolded statement mean?

I use this statement to know I am pointing at the first element in the list. If p_ship->p_last_enemy is null. Then I know it is the first element of the list. I use here in the delete an element function :
1
2
3
4
5
6
7
8
9
10
11
12
if(p_current->p_last_enemy==NULL)
 {
 p_current->p_next_enemy->p_last_enemy = NULL;
  delete p_current;
}
 else 
{
p_current->p_last_enemy->p_next_enemy = p_current->p_next_enemy;
 if(p_current->p_next_enemy!=NULL)
  p_current->p_next_enemy->p_last_enemy = p_current->p_last_enemy;
 delete p_current;
  }


Otherwise if it is not the first element of the list the right value is set at line 93 in the main.
p_enemies->p_next_enemy->p_last_enemy = p_enemies;

In the same way that addNewEnemyToList can result in a new list "head", so can delete_spaceship.

I suspect that's why you tell us that delete_spaceship is supposed to return a pointer to an EnemySpaceShip. It is too bad you don't follow up on that promise and return a pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
EnemySpaceShip* delete_spaceship(string name, EnemySpaceShip* p_list)
{
    EnemySpaceShip* current = p_list;

    while (current && current->ship_name != name)
        current = current->p_next_enemy;

    if (current)
    {
        if (current == p_list)
            p_list = current->p_next_enemy;
        else
            current->p_last_enemy->p_next_enemy = current->p_next_enemy;
        
        if (current->p_next_enemy)
            current->p_next_enemy->p_last_enemy = current->p_last_enemy;

        delete current;
    }
        
    return p_list;
}

// Meanwhile, in main...

    while (1)
    {
        cout << "Do yo wish to delete an enemy space ship?" << endl;
        cin >> yesorno;
        if (yesorno == "no")
            break;
        else
        {
            if (p_enemies)
            {
                cout << "What is the name of the enemy space ship you want to be deleted?" << endl;
                cin >> name;
                p_enemies = delete_spaceship(name, p_enemies);
                display_list(p_enemies);
            }
            else
                cout << "The list is empty.\n";
        }
    }
Topic archived. No new replies allowed.