FAQ |
Login

Circular Linked Lists implementing Iterable in Java?

Layer-visible-off
0
Unfavorites
0

We are working on the Josephus problem in our class, and this is what out professor has us doing for the assigment:

Design and implement, from first principles- e.g., you may not employ java.util.LinkedList- your own custom, link-based class for circular lists; specifically, make public class CircularList implements Iterable. Note that this class does not have to implement java.util.Collection, but it must correctly implement java.lang.Iterable, although iterators for your circular lists should continually traverse the list until there are no more items. Consequently, unless the list is empty, an iterator’s hasNext() method should return true. The rest of the design decisions are up to you, but you must rationalize them in your write-up.

Finally, your class must be “javadoc-able,” which is to say, if I were to run javadoc on your class, I would get a complete and meaningful guide to the use of your class, its constructors, and all of its methods.

Make JosephusSolver, a program that uses your circular list class to solve arbitrary instances of the Jospehus problem as articulated in class. Your program will be given n, the number of folks initially in the circle, and k, the skip factor. They should be non-negative integers, and they will be presented as args[0] and args[1], respectively. The names of the players, moving clockwise around the circle should be 1, 2, 3, etc. Each game commences with player 1, but the token/potato should move counter-clockwise around the circle (as opposed to the usual clockwise). The skip factor specifies the actual number of skips prior to the removal of a player, after which the token passes to the next player (which is slighty different from the usual rule). Finally, your program’s only output should name the last player standing.

Two examples: java JosephusSolver 7 3 should output 7; java JosephusSolver 3 7 should output 2.

Sorry for the long quote, I just wanted to give you the whole picture. But, I have the Josephus Solver class set, but I am having trouble in terms of Implementing Iterable. Here is all I have so far for my CircularLinkedList class:

public class CircularLinkedList implements Iterable{

private Node first;

private static class Node
{
    int value;
    Node next;

    public Node(int value, Node next)
    {
    this.value = value;
    this.next = next;
    }
}
public void remove()
{

}

public void append()
{

}

public boolean hasNext()
{
    return true;
}
}

I know I am on the right track somehow, but I am stuck with how to implement Iterable with the CircularLinkedList.

I don’t want someone to solve my code for me, because I don’t learn anything, but instead, if someone could tell me what I should be doing instead of what I am doing, or if someone could tell me how I should go about the methods and go about using the Iterator and Iterable, that would be greatly appreciated. Thanks in advance.