import java.util.Scanner; public class MyArrayBndQueue { public static final int CAPACITY = 3; private T[] queue = (T[]) new Object[CAPACITY]; private int numElts = 0; private int front = 0; private int rear = queue.length-1; 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; } public boolean isFull() { return numElts == queue.length; } public boolean isEmpty() { return numElts == 0; } // 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(); 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 -Xlint:unchecked MyArrayBndQueue.java" // End: