Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7935395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:54:26+00:00 2026-06-03T21:54:26+00:00

I understand that there are better ways to do what I’m attempting, but this

  • 0

I understand that there are better ways to do what I’m attempting, but this is a question in a book that I’m using to learn C++ and it will help me grasp some of the fundamentals before I move on. Anyway, here is my code:

#include <iostream>

using namespace std;

struct EnemySpaceShip
{
    int weapon_power;
    int xcoord;
    int ycoord;
    EnemySpaceShip *nextEnemy;
};

EnemySpaceShip* getNewEnemy(EnemySpaceShip* p_enemies) // Creates a new EnemySpaceShip in linked list p_enemies
{
    EnemySpaceShip *p_ship = new EnemySpaceShip;
    p_ship->xcoord = 0;
    p_ship->ycoord = 0;
    p_ship->weapon_power = 10;
    p_ship->nextEnemy = p_enemies;
    p_enemies = p_ship;
    return p_ship;
}

EnemySpaceShip* findPreRemove(EnemySpaceShip* p_enemies, int x_attack, int y_attack) // finds the element that is before the ship to be removed or returns NULL
{
    EnemySpaceShip *p_current = p_enemies;
    EnemySpaceShip *initialShip = p_enemies;
    int i= 0;
    while (p_current != NULL)
    {
        i++;
        if (p_current->xcoord == x_attack && p_current->ycoord == y_attack)
        {
            if (i == 1)
            {
                delete initialShip;
                delete p_current;
                return NULL;
            }
            else
            {
                for (int j = 1; j < i - 1; j++)
                {
                    initialShip = initialShip->nextEnemy;
                }
                delete p_current;
                return initialShip;
            }
        }
        p_current = p_current->nextEnemy;
    }
    return NULL;
}

EnemySpaceShip* findRemove(EnemySpaceShip* p_enemies, int x_attack, int y_attack)
{
    EnemySpaceShip *p_current = p_enemies;
    while (p_current != NULL)
    {
        if (p_current->xcoord == x_attack && p_current->ycoord == y_attack)
        {
            return p_current;
        }
        p_current = p_current->nextEnemy;
    }
}

EnemySpaceShip* removeEnemyShip(EnemySpaceShip *p_ship) // deletes the ship parameter and returns the ship after it in the list
{
    EnemySpaceShip *enemyAfterRemove = new EnemySpaceShip;
    enemyAfterRemove = p_ship->nextEnemy;
    delete p_ship;
    return enemyAfterRemove;
}

int main()
{
    EnemySpaceShip *p_enemies = NULL;
    EnemySpaceShip *Ship1 = getNewEnemy(p_enemies);
    EnemySpaceShip *Ship2 = getNewEnemy(p_enemies);
    EnemySpaceShip *Ship3 = getNewEnemy(p_enemies);

    Ship3->xcoord = 5; //arbitrary numbers to test the code
    Ship3->ycoord = 5;

    EnemySpaceShip *ShipBeforeRemove = findPreRemove(p_enemies, 5, 5);
    EnemySpaceShip *ShipToRemove = findRemove(p_enemies, 5, 5);
    ShipBeforeRemove->nextEnemy = removeEnemyShip(ShipToRemove);
}

I’m using arbitrary values to test the program, I don’t need to fully implement this as a functionality for the game that it is apparently being used for. Any help is much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:54:27+00:00Added an answer on June 3, 2026 at 9:54 pm

    I hope following is what you want.. Removing an element from a singly linked list..

    EnemySpaceShip* removeFromList(EnemySpaceShip* p_enemies, int x_attack, int y_attack)
    {
        if(p_enemies == NULL)
            return NULL;
    
        EnemySpaceShip *p1 = p_enemies;
        EnemySpaceShip *p2 = p1->next;
    
        if(p2 == NULL)
        {
            // Trivial case..
    
            if(p1->xcoord == x_attack && p1->ycoord == y_attack)
            {
                delete p_enemies;
                p_enemies = NULL;
            }
    
            return NULL;
        }
    
        while(p2 != NULL)
        {
             if(p2->xcoord == x_attack && p2->ycoord == y_attack)
             {
                 // Element found, remove it and assign its previous pointer
                 // as the next pointer of the deleted one..
                 p1->next = p2->next;
                 delete p2;
                 return p1;
             }
             else
             {
                 p1 = p2;
                 p2 = p2->next;
             }
        }
    
        return NULL;
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just to preface my question, I understand that there is no direct support for
Why only one overload throws this exception? Little update: I understand that there was
I understand that there is a setting in Sun JVM for dumping the heap
Are there overall rules/guidelines for what makes a method thread-safe? I understand that there
I am trying to understand the co-ordinate spaces of three.js, I understand that there
Is there a way to debug CoffeeScript line-by-line? I understand that it compiles into
There are things that I don't understand when it comes to linking... I'm writing
There is something that I really don't understand with the HttpListener. The code below
I'm beginning and there is something that I don't understand with pointers. I have
I would like to use the WebClient (or there is another better option?) but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.