// ArrayQueue.java import java.util.Scanner; // a queue based on an array class Queue { private int[] array; private int start; private int end; private int size; // constructor public Queue(int capacity) { array = new int[capacity]; start = -1; end = -1; size = 0; } // enqueue - add at end public void enqueue(int number) { end++; if (end == array.length) { end = 0; } array[end] = number; size++; if (start == -1) { start = end; } } // dequeue - remove from start public int dequeue() { int returnVal = array[start]; start++; if (start == array.length) { start = 0; } size--; if (size == 0) { start = -1; end = -1; } return returnVal; } // get the size public int getSize() { return size; } // return whether or not we're empty public boolean empty() { return size == 0; } // return whether or not we're full public boolean full() { return size == array.length; } } public class ArrayQueue { public static void main(String args[]) { Queue buffer = new Queue(10); Scanner in = new Scanner(System.in); int num; do { num = in.nextInt(); buffer.enqueue(num); if (buffer.full()) { System.out.print("Numbers: "); while (!buffer.empty()) { // do something with the buffered numbers System.out.print(buffer.dequeue() + " "); } System.out.println(); } } while (num > 0); } }