Linked list stumped

Hi All,

I am trying to get my head around linked lists. But not got a clue why my code isnt updating the p_head of list. It looks like within the function its returning the right information. But when the display list is executed again it uses the old information. Cant figure out why at all tried all sorts.

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
#include <iostream>

using namespace std;

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

EnemySpaceShip* p_enemies = NULL;

EnemySpaceShip* Delete_Enemies (string name, EnemySpaceShip *p_list)
{
    
    EnemySpaceShip* p_current = p_list;
    
    if ( p_current->ship_name == name){
        cout << "Last ship1 - " << p_current->ship_name << "\n";
        cout << "Current p_list - " << p_list << "\n";
        p_list = p_current->p_next_enemy;
        cout << "Is p_list updated - " << p_list << "\n";
        delete p_current;
        return p_list;
  
        
    }
    
    while( p_current != NULL)
    {
        
        
        if ( p_current->p_next_enemy != NULL ){
            
            if ( p_current->p_next_enemy->ship_name == name ){
                
                cout << "Current ship - " << p_current->ship_name << "\n";
                cout << "Found ship - " << p_current->p_next_enemy->ship_name << "\n";
                p_current->p_next_enemy = p_current->p_next_enemy->p_next_enemy;
                // delete p_current->p_next_enemy->p_next_enemy;
                
            }
            
            
        }
        
        p_current = p_current->p_next_enemy;
        
        
    }
    cout << "p_list " << p_list << "\n";
    return p_list;
}
EnemySpaceShip* getNewEnemy (string name)
{
    EnemySpaceShip* p_ship = new EnemySpaceShip;
    p_ship->ship_name = name;
    p_ship->x_coordinate = 0;
    p_ship->y_coordinate = 0;
    p_ship->weapon_power = 20;
    p_ship->p_next_enemy = p_enemies;
    p_ship->p_current_enemy = p_ship;
    p_enemies = p_ship;
    return p_ship;
}


void upgradeWeapons (EnemySpaceShip* p_ship)
{
    p_ship->weapon_power += 10;
    
}

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





int main ()
{

    string ee;
    string sships[]= { "BattleStar", "Starship", "Enterprise", "Warbird", "SG1" };
    EnemySpaceShip* BattleStar = getNewEnemy (sships[0]);
    EnemySpaceShip* Starship = getNewEnemy (sships[1]);
    EnemySpaceShip* Enterprise = getNewEnemy (sships[2]);
    EnemySpaceShip* Warbird = getNewEnemy (sships[3]);
    EnemySpaceShip* SG1 = getNewEnemy (sships[4]);
  
    display_list(p_enemies);
    Delete_Enemies(sships[4],p_enemies);
    cout << p_enemies << "\n";
    display_list(p_enemies);
}   
The delete enemies function returns a pointer but you don't save it here
Delete_Enemies(sships[4],p_enemies);

Do you mean to update p_enemies with the returned pointer? p_enemies is still pointing to the last ship added, which is the one that is then deleted.

With linked lists, I find it helps to draw it out with pen and paper and follow what is happening with the pointers at each step.
Thanks for the reply wild blue. Yes, I made a massive school boy error but I guess thats the joys of learning. I eventually got to that function was returning a value but I didnt update the head of the linked list.

So to get it working I did :

1
2
3

p_enemies = Delete_Enemies(sships[4],p_enemies);


Feel like a bit of a pleb :(

Really appreciate the help and your reply!
Topic archived. No new replies allowed.