Outputting data in a Linked List

Hey.

I'm trying to get this linked list to work in a way that allows me to output the values at line 34, I've tried the standard deference operator to no avail and a quick Google search has draw a blank. Any help would be appreciated as would general Linked List advice.

Thanks.

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
  struct zombie
{
public:
	int health;
	int speed;
	zombie* p_nextZombie;

	zombie::zombie()
	{
		int health = 10;
		int speed = 10;
		zombie* p_nextZombie = NULL;
	}
};

zombie* zombieHead;

int main()
{

	zombie* p_zombie1 = new zombie;
	zombieHead = p_zombie1;

	zombie* p_zombie2 = new zombie;
	p_zombie1->p_nextZombie = p_zombie2;
	p_zombie2->p_nextZombie = NULL;

	zombie* X = zombieHead;

	if (X != NULL)
	{
		while (X->p_nextZombie != NULL)
		{
			std::cout << "Health: " << X->health << std::endl << "Speed: " << X->speed << std::endl;
			X = X->p_nextZombie;
		}
		
	}

	std::cin.get();
	delete p_zombie1;
	delete p_zombie2;

	return 0;
}
Last edited on
Try getting rid of lines 32, 33, and 36 and change line 30 to a while instead of an if.
Your main problem is that you've locally declared health and speed in the constructor for zombie, instead of assigning the values to your structure members.

the constructor should be:
1
2
3
4
5
6
zombie()
{
    health = 10;
    speed = 10;
    p_nextZombie = NULL;
}
Cheers guys. I've managed to get it working now.
Topic archived. No new replies allowed.