logo CodeStepByStep logo

eliminateFromRing

Language/Type: C++ linked lists pointers
Related Links:

Write a function named eliminateFromRing that accepts a reference to a pointer to an AssassinNode representing the front of a linked list for a game called Assassin, along with a string representing a player's name, and removes that player from the game. (This task involves removing a given element value from a linked list.)

Assassin is a game played on college campuses where each player has a "target" they are pursuing. If the player finds and touches their target, that target is eliminated from the game. The last person left in the game is the winner.

The AssassinNode structure is defined as follows:

struct AssassinNode {
    string data;          // name of this person
    AssassinNode* next;   // pointer to next node in list (nullptr if none)
};

Your function is passed the front of the list and the name of the person to eliminate. For example, if a variable named front points to the front of the following list of nodes:

front -> "Joe" -> "Erica" -> "Tad" -> "Phoebe" -> "Ruth" -> "Bobby" -> "Anita" /

Then the call of eliminateFromRing(front, "Phoebe"); should modify the linked list's contents to the following:

front -> "Joe" -> "Erica" -> "Tad" -> "Ruth" -> "Bobby" -> "Anita" /

If the name passed does not match any player in the game, do not modify the list.

You must avoid memory leaks by calling delete on any eliminated node.

Constraints: Do not construct any new AssassinNode objects in solving this problem (though you may create as many node pointer variables as you like). Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). Your function should not modify any node's data value.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.