// Object ring (simplified queue) // // Author Matthew Caryl // // Although under copywrite to the author (Matthew Caryl) this code can be copied and modified for non-commercial // purposes as long as any derivatives contain this condition. package ADT; final public class Ring { Object[] ring = null; int current; // copy order of elements from queue into ring public Ring(Queue source) throws QueueEmpty { int s = source.size(); int i; if (source.empty()) throw new QueueEmpty(); ring = new Object[s]; try { for (i = 0; i < s; i++) ring[i] = source.requeue(); } catch (QueueEmpty e) { } current = 0; } // returns the object at the front of the ring public Object peekqueue() { return ring[current]; } // returns the object at the front of the ring // and moves onto the next object public Object requeue() { Object o; o = ring[current]; current++; if (current >= ring.length) current = 0; return o; } public boolean membership(Object o) { int p, q; p = 0; q = ring.length; while (p != q) if (ring[p] == o) q = p; else p++; return p != ring.length; } public int size() { return ring.length; } }