Hello and I hope all is well. I am trying to remove the third node from the linked list and then linking the second and fourth nodes together. However, I'm not sure how to accomplish this.
Here is my line of reasoning:
1. Traverse to the third linked list.
2. Delete it.
3. Change the second linked list's pointer to point to the address of the first element in the fourth linked list.
First, I'm not sure how to delete the third linked list... I tried using the delete syntax but it didn't work. Or maybe, I just don't know how to write it correctly.
Second, I have no idea how to accomplish the third step. Haha...
Those three steps are the way to remove a node from the singly linked list. You should use the term node instead of list since you have just one list with several nodes.
1 2 3 4 5 6 7 8 9 10 11
EnemySpaceShip *prev_enemy = nullptr; // This will be the second node later
//traverse to the third node.
for (int i = 0; i < 2; i++)
{
prev_enemy = p_enemies; // Keep this
p_enemies = p_enemies->p_next_enemy;
}
// Now prev_enemy is the second node and p_enemies the third
prev_enemy->p_next_enemy = p_enemies->p_next_enemy; // This unlinks the third node -> The fourth node is now the next of the second node.
delete p_enemies; // Since p_enemies is no longer in the list it can be safely deleted.
Awesome! Thank you for the response. I will make sure to change my terminology. Now, how would I check if the node was actually deleted and the second and fourth nodes are now connected?
I would assume you would have to traverse back to the first node and then cout each node from there?