Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Circular Linked List HELP NEEDED! In a distant land, the death penalty is still in force. The king of the land, who likes to

C++ Circular Linked List HELP NEEDED!

In a distant land, the death penalty is still in force. The king of the land, who likes to think that he is a nice chap, has made the execution of the prisoners part of the yearly Games. At the end of the Games, all the prisoners from death row are put in a circle in the arena. Guards with spears ensure that no prisoner can move out of the circle. One of the prisoners is designated number 1. Then the executioner takes position at the center of the circle, and starts reciting a rhyme he remembers from his childhood: 'EENIE MEENIE MINIE MOE, I THINK FOR YOU IT IS TIME TO GO'. For each word of the rhyme he aims at a different person, starting with prisoner 1 for 'EENIE', prisoner 2 for 'MEENIE', etc. (thus with each word he moves his aim to the right, or clockwise.) The prisoner he aims at with 'GO' is then executed, and, during the cheers of the crowd, removed from the scene. At the next reciting of the rhyme, the executioner starts at the neighbor of the most recently deceased, continuing to the executioner's right; i.e. the executioner begins at the next clockwise neighbor of the deceased.

The king (I told you he is a nice chap), releases the one prisoner left standing at the end of the game.

For example, if there were six prisoners and the rhyme only contained 3 words then initially the prisoners are arranged as:

 123456 (with 1 and 6 next to each other in the circle)

The executioner counts off three words to end up on prisoner number 3 who is executed:

 12456 

The executioner is now on prisoner 4, and counts off another three words to end up on prisoner number 6 who is then executed:

 1245

The executioner is now on prisoner 1, and counts off another three words to end up on prisoner 4 who is then executed:

 125

The executioner is now on prisoner 5, and counts off another three words to end up on prisoner 2 who is then executed:

 15

The executioner is now on prisoner 5 and counts off another three words to end up on prisoner 5 who is then executed:

 1

Prisoner 1 is the survivor!

If you would ever participate in this "game," you will want to improve your chances by determining beforehand who is going to be released. To be able to calculate that, you need to know the number of prisoners and the length in words of the rhyme used by the executioner for this particular game. It might be nice to have a program that calculates the position quickly for you (maybe on the computer they let you use in prison). Write a program that inputs the number of prisoners and the number of words and calculates where you should stand. For example, print the following text: 'With P prisoners and S words, I'd like to be number X.', where X is the prisoner index of the one surviving (the prisoners are numbered from 1 through P in the direction of the aim of the executioner).

Example Output:

 With 3 prisoners and 3 words, I'd like to be number 2. With 6 prisoners and 2 words, I'd like to be number 5. With 10 prisoners and 1 words, I'd like to be number 10. 

You must use a CIRCULAR LINKED LIST of a Prisoner class to simulation the position of each prisoner in the "game" and calculate the correct position to survive. The Prisoner class only needs to store the number of the prisoner and a "next" pointer. (You could also have a "previous" pointer if you want to make a doubly-linked list). Your program should play the game, deleting the node in the circular linked list that corresponds to the prisoner that is executed.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions