linked list issue

I'm trying to get a good grip on pointers and linked lists, this is the first list prog I made. I know passing in and searching for a name probably isn't the best way to do it, but I'm mostly just experimenting

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

using namespace std;

struct SpaceShip
{
    string name;
    int health;
    int power;
    int fuel;
    SpaceShip* nextShip;
};

SpaceShip* Shiplist = NULL;

void newSpaceShip ( string NewName )
{
    SpaceShip* ShipHold = new SpaceShip;
    ShipHold->name = NewName;
    ShipHold->health = 20;
    ShipHold->power = 30;
    ShipHold->fuel = 100;
    ShipHold->nextShip = Shiplist;
    Shiplist = ShipHold;
}

SpaceShip* findShip ( string ShipFind )
{
    SpaceShip* cur = Shiplist;
    while ( cur -> name != ShipFind && cur != NULL )
    {
        cur = cur->nextShip;
    }
    return cur;
}

void UpgradeHealth ( string ShipUpgrade )
{
    SpaceShip* temp = findShip ( ShipUpgrade );
    temp -> health += 5;
}



int main()
{
    newSpaceShip ( "Scar" );
    cout << Shiplist->name;
    newSpaceShip ( "Limber" );
    newSpaceShip ( "Nimble" );
    newSpaceShip ( "Cunning" );
    UpgradeHealth( "Limber" );
    cout << Shiplist->name;
    SpaceShip* testShip = findShip( "Limber" );
    cout << testShip->health;
}


What I tried to do was make it so that I can pass in any list I please and it will add the new ship to that specific list with some global lists made to test it:

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

using namespace std;

struct SpaceShip
{
    string name;
    int health;
    int power;
    int fuel;
    SpaceShip* nextShip;
};

SpaceShip* ShiplistBad = NULL;
SpaceShip* ShiplistGood = NULL;

void newSpaceShip ( string NewName, SpaceShip* plist )
{
    SpaceShip* ShipHold = new SpaceShip;
    ShipHold->name = NewName;
    ShipHold->health = 20;
    ShipHold->power = 30;
    ShipHold->fuel = 100;
    ShipHold->nextShip = plist;
    plist = ShipHold;
}

SpaceShip* findShip ( string ShipFind, SpaceShip* plist )
{
    SpaceShip* cur = plist;
    while ( cur -> name != ShipFind && cur != NULL )
    {
        cur = cur->nextShip;
    }
    return cur;
}

void UpgradeHealth ( string ShipUpgrade, SpaceShip* plist )
{
    SpaceShip* temp = findShip ( ShipUpgrade, plist );
    temp -> health += 5;
}



int main()
{
    newSpaceShip ( "Scar", ShiplistBad );
    cout << ShiplistBad->name;
    newSpaceShip ( "Limber", ShiplistGood );
    newSpaceShip ( "Nimble", ShiplistGood );
    newSpaceShip ( "Cunning", ShiplistBad );
    UpgradeHealth( "Limber", ShiplistGood );
    cout << ShiplistGood->name;
    SpaceShip* testShip = findShip( "Limber", ShiplistGood );
    cout << testShip->health;
}


However it doesn't seem to be working, it builds fine but then when running it outputs nothing and crashes. Not quite sure whats happening.
Some insight would be nice, pointers and linked lists are a bit confusing haha..
In newSpaceShip(...) plist is a copy of the pointer, so assigning it does nothing to the global pointer. Pass the pointer by reference void newSpaceShip ( string NewName, SpaceShip*& plist )
Thanks for the prompt reply. You were right haha. I realized after I implemented the changes though that I was going to have to change a lot and just overcomplicate things when it's not even necessary, I was just wanting to know how one could do it.

I added a few new functions to it and played around a bit more. I'm starting to understand more how it works, even was able to make a delete function which flexed my mind a bit.

Still have a few questions though
Here is the program now:

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

using namespace std;

struct SpaceShip
{
    string name;
    int health;
    int power;
    int fuel;
    SpaceShip* nextShip;
};

SpaceShip* Shiplist = NULL;

void newSpaceShip ( string NewName )
{
    SpaceShip* ShipHold = new SpaceShip;
    ShipHold->name = NewName;
    ShipHold->health = 20;
    ShipHold->power = 5;
    ShipHold->fuel = 100;
    ShipHold->nextShip = Shiplist;
    Shiplist = ShipHold;
    ShipHold = NULL;
}

SpaceShip* findShip ( string ShipFind )
{
    SpaceShip* cur = Shiplist;
    while ( cur -> name != ShipFind && cur != NULL )
    {
        cur = cur->nextShip;
    }
    if (cur == NULL) {cout << "Ship " << ShipFind << "was not found.";}
    return cur;
}

void UpgradeHealth ( string ShipUpgrade )
{
    SpaceShip* temp = findShip ( ShipUpgrade );
    temp -> health += 5;
}

void DestroyShip ( string Damaged )
{
    SpaceShip* delptr = NULL;
    SpaceShip* temp = Shiplist;
    SpaceShip* curr = Shiplist;
    while ( curr != NULL && curr -> name != Damaged )
    {
        temp = curr;
        curr = curr -> nextShip;
    }
        delptr = curr;
        curr = curr -> nextShip;
        temp -> nextShip = curr;
        delete delptr;
        cout << "The spaceship " << Damaged << " was destroyed in the battle." << endl;
}

void DamageShip ( int TimesAttack, string ShipAttack, string ShipDamage )
{
    SpaceShip* Attack = findShip ( ShipAttack );
    SpaceShip* Defend = findShip ( ShipDamage );
    Defend->health -= Attack->power * TimesAttack;
    if (Defend->health <= 0 )
        {
            DestroyShip ( ShipDamage );
        }
}

void displayList ()
{
    SpaceShip* curr = Shiplist;
    while ( curr != NULL )
    {
        cout << curr->name << curr->health << endl;
        curr = curr->nextShip;
    }
}

int main()
{}


inserted an attack funct, a destroy funct, and a display name and health funct.
Heres a few questions for you, or anyone checking this thread out, if anyone would be so kind.

I'm thinking of changing the destroy funct to accept the pointer to the node to be destroyed itself instead of the string name to avoid having to have two created nodes traverse the list together. Would it be worth it??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void DestroyShip ( string Damaged )
{
    SpaceShip* delptr = NULL;
    SpaceShip* temp = Shiplist;
    SpaceShip* curr = Shiplist;
    while ( curr != NULL && curr -> name != Damaged )
    {
        temp = curr;
        curr = curr -> nextShip;
    }
        delptr = curr;
        curr = curr -> nextShip;
        temp -> nextShip = curr;
        delete delptr;
        cout << "The spaceship " << Damaged << " was destroyed in the battle." << endl;
}


and another question. if I were to make the initial newSpaceShip function return a pointer to the newly created spaceship after adding it to the linked list instead of being void and having to call each ship through the findShip function later would that increase performance?

for example if I changed it to:
1
2
3
4
5
6
7
8
9
10
11
SpaceShip* newSpaceShip ()
{
    SpaceShip* ShipHold = new SpaceShip;
    ShipHold->name = NewName;
    ShipHold->health = 20;
    ShipHold->power = 30;
    ShipHold->fuel = 100;
    ShipHold->nextShip = Shiplist;
    Shiplist = ShipHold;
    return ShipHold;
}

I would only be creating a pointer object to the object in the linked list then right? That way I could pass it by pointer in main versus having to traverse the list and find the specific node each time. I initially made the code with extra steps on purpose just so I could get a better understanding and more practice of pointers but now I'm more curious for next time what is the more efficient method.

Final question, I only have to set a pointer to NULL if I allocated memory for it and no longer need that memory right? When I set a pointer to point to an object in a function it would be a waste to NULL that pointer as the last step of the function?

I hope that makes sense, and thanks in advance. I'm really just trying to get a deep understanding of this stuff

Last edited on
Topic archived. No new replies allowed.