Question: Need help with this java program have to create the circular linked list. N people numbered 1 to N are sitting in a circle. Starting
Need help with this java program have to create the circular linked list. N people numbered 1 to N are sitting in a circle. Starting at person 1, a hot potato is passed. After M passes, the person holding the hot potato is eliminated, the circle closes rank, and the game continues with the person who was sitting after the eliminated person picking up the hot potato. The last remaining person wins. For example, if M =1, and N= 5 the order of elimination is 2, 4, 1, and 5. 3 is the winner.
Write a program to implement the Josephus problem using a LinkedList. You are not allowed to use Java API list classes. Your program should accept M and N as inputs. It then constructs a circular list with N elements numbered 1 through N then implements a method called josephus .
Your program should use the input/output format given in the two examples below. Points will be deducted if you do not adhere to this format.
Enter the values of M: 1 Enter the value of N: 5 Current list: 1 2 3 4 5 Pass 1 eliminated 2 Current list: 1 3 4 5 Pass 2 eliminated 4 Current list: 1 3 5 Pass 3 eliminated 1 Current list: 3 5 Pass 4 eliminated 5 Current list: 3 WINNER: 3
Enter the values of M: 2 Enter the value of N: 6 Current list: 1 2 3 4 5 6 Pass 1 eliminated 3 Current list: 1 2 4 5 6 Pass 2 eliminated 6 Current list: 1 2 4 5 Pass 3 eliminated 4 Current list: 1 2 5 Pass 4 eliminated 2 Current list: 1 5 Pass 5 eliminated 5 Current list: 1 WINNER: 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
