Problem
The Josephus Problem is a famous mathematical puzzle that goes back to ancient times. There are many stories to go with the puzzle. One is that Josephus was one of a group of Jews who were about to be captured by the Romans. Rather than be enslaved, they chose to commit suicide. They arranged themselves in a circle and, starting at a certain person, started counting off around the circle. Every nth person had to leave the circle and commit suicide. Josephus decided he didn’t want to die, so he arranged the rules so he would be the last person left. If there were (say) 20 people, and he was the seventh person from the start of the circle, what number should he tell them to use for counting off? The problem is made much more complicated because the circle shrinks as the counting continues.
Create an application that uses a circular linked list (like that in Programming Project 5.3) to model this problem. Inputs are the number of people in the circle, the number used for counting off, and the number of the person where counting starts (usually 1). The output is the list of persons being eliminated. When a person drops out of the circle, counting starts again from the person who was on his left (assuming you go around clockwise). Here’s an example. There are seven people numbered 1 through 7, and you start at 1 and count off by threes. People will be eliminated in the order 4, 1, 6, 5, 7, 3. Number 2 will be left.
I have managed to solve the following problem but I am afraid my code might contain bad practices or there might be ways to improve it. I would appreciate if someone can point out how I can make my code better as it will help me learn to write more beautiful code.
Node
Class
public class Node {
public int iData;
public Node next;
public Node(int x) {
iData = x;
}
public void displayNode() {
System.out.print(iData + " ");
}
}
Code for Circular Linked List
public class CircularList {
private Node first;
private Node last;
private Node current;
private int count; // total items in the list
public CircularList getCurrent;
public CircularList() {
first = null;
last = null;
current = null;
count = 0;
}
public boolean isEmpty() {
return first == null;
}
public void step() {
current = current.next;
}
public Node getCurrent() {
return current;
}
public Node getFirst() {
return first;
}
public void insert(int x) {
Node newNode = new Node(x);
if (isEmpty()) {
first = newNode;
current = first;
} else {
current.next = newNode;
}
newNode.next = first;
last = newNode;
step();
count++;
}
public boolean search(int x) {
Node search = first;
int y = 0;
while (search.iData != x && y < count) {
search = search.next;
y++;
}
if (search.iData == x) {
System.out.println("Found the value: " + search.iData);
return true;
} else {
System.out.println("Value not found in list");
return false;
}
}
public void delete(int x) {
Node prev = first;
Node curr = first.next;
while (curr.iData != x) {
prev = curr;
curr = curr.next;
}
if (count == 1) {
first = null;
count--;
} else if (curr == first) {
prev.next = curr.next;
first = curr.next;
count--;
} else {
prev.next = curr.next;
count--;
}
}
public void displayList() {
int x = 0;
Node printer = first;
while (x < count) {
printer.displayNode();
printer = printer.next;
x++;
}
System.out.println("");
}
}
Josephus
Class
public class Josephus {
private int numOfPeople; // number of people in a circle
private int countNum; // number used for counting off
private Node head;
private Node tail;
CircularList circle;
public Josephus() {
circle = new CircularList();
numOfPeople = 0;
countNum = 0;
}
public void setNumOfPeople(int x) {
numOfPeople = x;
}
public int getNumOfPeople() {
return numOfPeople;
}
public void setCountNum(int x) {
countNum = x;
}
public int getCountNum() {
return countNum;
}
public void addPeople() {
for (int i = 1; i < numOfPeople; i++) {
circle.insert(i);
}
}
public void move() {
for (int i = 0; i < countNum; i++) {
tail = head;
head = head.next;
}
System.out.println("KILLED : " + head.iData);
}
public void execute() {
tail = null;
head = circle.getFirst();
while (numOfPeople != 2) {
move();
circle.delete(head.iData);
tail = tail.next;
head = head.next;
numOfPeople--;
display();
}
}
public void display() {
System.out.print("Alive: ");
circle.displayList();
}
}
Main Class
public class Test {
public static void main(String[] args) {
Josephus suicide = new Josephus();
suicide.setNumOfPeople(8);
suicide.addPeople();
suicide.display();
suicide.setCountNum(3);
suicide.execute();
}
}
Output
Alive: 1 2 3 4 5 6 7 KILLED : 4 Alive: 1 2 3 5 6 7 KILLED : 1 Alive: 2 3 5 6 7 KILLED : 6 Alive: 2 3 5 7 KILLED : 5 Alive: 2 3 7 KILLED : 7 Alive: 2 3 KILLED : 3 Alive: 2