import java.util.Scanner; public class MyArrayBndQueue { private T[] queue; private int numElts = 0; private int front = 0; private int rear = -1; public MyArrayBndQueue(T[] queue) { this.queue = queue; } public boolean isFull() { return numElts == queue.length; } public boolean isEmpty() { return numElts == 0; } public T dequeue() throws Exception { if (isEmpty()) throw new Exception ("Dequeue on empty Q"); numElts--; T ans = queue[front]; front = (front+1) % queue.length; return ans; } public void enqueue(T elt) throws Exception { if (isFull()) throw new Exception ("Enqueue on full Q"); numElts++; rear = (rear+1) % queue.length; queue[rear] = elt; } // The class should end here. Main program from end of 5.2 public static void main (String[] args) throws Exception { Scanner getInput = new Scanner(System.in); String str; MyArrayBndQueue queue = new MyArrayBndQueue(new Integer[3]); while (!(str=getInput.next()).equals("done")) if (str.equals("enqueue")) queue.enqueue(getInput.nextInt()); else if (str.equals("dequeue")) System.out.println(queue.dequeue()); else System.out.println("Illegal!"); } } // Local Variables: // c-basic-offset: 4 // compile-command: "javac MyArrayBndQueue.java" // End: