// Textbook fragment 05.16 import net.datastructures.*; public class Josephus { /** Solution of the Josephus problem using a queue. */ public static E Josephus(Queue Q, int k) { if (Q.isEmpty()) return null; while (Q.size() > 1) { System.out.println(" Queue: " + Q + " k = " + k); for (int i=0; i < k; i++) Q.enqueue(Q.dequeue()); // move the front element to the end E e = Q.dequeue(); // remove the front element from the collection System.out.println(" " + e + " is out"); } return Q.dequeue(); // the winner } /** Build a queue from an array of objects */ public static Queue buildQueue(E a[]) { Queue Q = new NodeQueue(); for (int i=0; i