Linked List Mayhem!
Nov 5, 2014 at 8:55pm UTC
Hi All
Just learning about linked list and doing some excersises feel like I have hit a brick wall. There are two things one is when you self reference within a struct and then return does that link to the entire pointer to the struct for example p_head?
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
struct EnemySpaceShip
{
string ship_name;
int x_coordinate;
int y_coordinate;
int weapon_power;
EnemySpaceShip* p_next_enemy;
};
EnemySpaceShip* p_head = NULL;
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 = 0;
p_ship->p_next_enemy = p_head;
p_head = p_ship;
return p_ship;
}
The other is don't understand how the same value makes two different results. Am I missing some fundamentals here?
1 2
next = current->p_last_enemy; // cout produces 0x1001054f0
current->p_last_enemy = prev; // cout produces 0x100105570
Any help would be greatly appreciated.
Nov 5, 2014 at 10:03pm UTC
1 2
next = current->p_last_enemy; // cout produces 0x1001054f0
current->p_last_enemy = prev; // cout produces 0x100105570
Dint see them in your code plz add the full code
Nov 5, 2014 at 10:15pm UTC
Hi heres the full code
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
#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_last_enemy;
EnemySpaceShip* p_current_enemy;
};
EnemySpaceShip* p_enemies = NULL;
EnemySpaceShip* p_enemies1 = NULL;
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 = 0;
p_ship->p_next_enemy = p_enemies;
p_ship->p_last_enemy = p_enemies1;
p_ship->p_current_enemy = p_ship;
p_enemies = p_ship;
p_enemies1 = p_ship;
return p_ship;
}
void display_list(EnemySpaceShip *p_list)
{
EnemySpaceShip *p_current = p_list;
// p_current = p_current->p_next_enemy;
while (p_current!=NULL)
{
cout << " Ship Name : " << p_current->ship_name << " Current Address : " << p_current->p_current_enemy << " Previous Enemy : " <<
p_current->p_next_enemy << " Next Address : " << p_current->p_last_enemy << "\n" ;
p_current = p_current->p_next_enemy;
}
}
EnemySpaceShip* Reverse (struct EnemySpaceShip* head)
{
EnemySpaceShip *current, *prev, *next;
current = head;
prev = NULL;
while (current != NULL)
{
next = current->p_last_enemy;
current->p_last_enemy = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
int main ()
{
string sships[]= { "ship1" , "ship2" , "ship3" , "ship4" , "ship5" , "ship6" , "ship7" };
EnemySpaceShip* p_ship1 = getNewEnemy(sships[0]);
EnemySpaceShip* p_ship2 = getNewEnemy(sships[1]);
EnemySpaceShip* p_ship3 = getNewEnemy(sships[2]);
EnemySpaceShip* p_ship4 = getNewEnemy(sships[3]);
EnemySpaceShip* p_ship5 = getNewEnemy(sships[4]);
EnemySpaceShip* p_ship6 = getNewEnemy(sships[5]);
EnemySpaceShip* p_ship7 = getNewEnemy(sships[6]);
display_list(p_enemies);
cout << "--------------- \n" ;
Reverse(p_enemies1);
display_list(p_enemies1);
}
Topic archived. No new replies allowed.