logo CodeStepByStep logo

printTargetRing

Language/Type: C++ linked lists pointers

Write a function named printTargetRing that accepts a pointer to an AssassinNode representing the front of a linked list for a game called Assassin and prints information about the players in the game. (This task is essentially just looping over a linked list and printing it, with a wrap-around to the front element.)

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)
};

The list is organized such that each person is pursuing the next person in the list. The last person in the list is pursuing the first (front) person in the list. 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 printTargetRing(front); should print the following output:

Joe is pursuing Erica
Erica is pursuing Tad
Tad is pursuing Phoebe
Phoebe is pursuing Ruth
Ruth is pursuing Bobby
Bobby is pursuing Anita
Anita is pursuing Joe

There are two special cases. If the list is empty (null), print no output. If the list contains only a single node, such as "Phoebe", print:

Phoebe is the winner!

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 the linked list's state; the state of the list should remain constant with respect to your function.

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.