Question Regarding Linked Lists

Hello, I am trying to understand linked lists. The following code is from a book.

My question is, what does "EnemySpaceShip *getNewEnemy ()" mean? I understand this statement if it were written like so: "EnemySpaceShip *getNewEnemy". That this is a pointer to a structure named EnemySpaceShip. However, I lose my understanding when the statement includes () like so "EnemySpaceShip *getNewEnemy ()". Any suggestions on how to understand this statement?

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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std; 

struct EnemySpaceShip
{
	int x_coordinate; 
	int y_coordinate; 
	int weapon_power;
	EnemySpaceShip *p_next_enemy; 	
};

EnemySpaceShip *p_enemies = NULL; 

EnemySpaceShip *getNewEnemy ()
{
	EnemySpaceShip *p_ship = new EnemySpaceShip;
	p_ship->x_coordinate = 0; 
	p_ship->y_coordinate = 0; 
	p_ship->weapon_power = 20; 
	p_ship->p_next_enemy = p_enemies; 
	p_enemies = p_ship; 
	return p_ship; 
}

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

int main () 
{ 
	EnemySpaceShip *p_enemy = getNewEnemy (); 
	upgradeWeapons (p_enemy); 
    return 0; 
}
line17: EnemySpaceShip *getNewEnemy ()
here the function name is getNewEnemy and it returns a pointer of type EnemySpaceShip. notice that it returns p_ship.

it's more clear to understand: EnemySpaceShip* getNewEnemy ()
@anup30

Thank you for your response. One another question. What does "p_enemies = p_ship" mean? I don't think this is dereferencing a p_enemies because to dereference it, I need to use "*", that is, *p_enemies, to dereference. So what is this statement saying?

when the getNewEnemy function is called, each time it creates a new "head" of the "linked list chain".

its a bad example(book, JiCpp). i would you suggest a different book/material.

@anup30

I see. Thank you for your response. What book would you suggest?
Topic archived. No new replies allowed.