ProblemThe 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 didnt  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). Heres 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 Classpublic 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 Listpublic 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 Classpublic 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 Classpublic class Test {    public static void main(String[] args) {        Josephus suicide = new Josephus();        suicide.setNumOfPeople(8);        suicide.addPeople();        suicide.display();        suicide.setCountNum(3);        suicide.execute();    }}OutputAlive:  1 2 3 4 5 6 7 KILLED : 4Alive:  1 2 3 5 6 7 KILLED : 1Alive:  2 3 5 6 7 KILLED : 6Alive:  2 3 5 7 KILLED : 5Alive:  2 3 7 KILLED : 7Alive:  2 3 KILLED : 3Alive:  2
Josephus Problem in Java
java;algorithm;linked list;circular list
[Not a review of your solution, since it does not exist yet, but a review of your CircularList so far.]It does not make much sense to have a first and last node in a circularly linked list.  Maybe you can keep a node as an entry point, but you should not make it visible.  You should actually not make any node accessible at all from CircularList.The search and delete methods are ambiguous, unless each value can only be present once in a CircularList.  If you decide do use this convention, you should check that a value is not a duplicate at insertion.Defining an insertion method is quite tricky.  About the only possibility I see is to insert some value before or after the first instance of some target value.  It is better to not have an insertion method.  You would only add values at construction; the constructor would take in a list of values.You can easily make CircularList generic.  It requires only a few modifications.