public class Queue { private int maxSize; private int[] queArray; private int front; private int rear; private int nItems; public Queue(int s) { maxSize = s; queArray = new int[maxSize]; front = 0; rear = 0; nItems = 0; } // put item at the rear of a queue public void enqueue(int j) { if (!isFull()) // deal with wraparound queArray[rear] = j; // insert rear=(rear+1) % maxSize; nItems++; } // take item from the front of queue public int dequeue() { if (isEmpty()) return 0; int temp = queArray[front]; // get value and incr front queArray[front]=0; front=(front+1) % maxSize; nItems--; // one less item return temp; } public int peekFront() { return queArray[front]; } public boolean isEmpty() { return (nItems == 0); } public boolean isFull() { return (nItems == maxSize); } public int size() { return nItems; } public static void main(String[] args) { Queue theQueue = new Queue(5); // queue holds 5 items theQueue.enqueue( 10); theQueue.enqueue(20); theQueue.enqueue(30); theQueue.enqueue(40); theQueue.dequeue(); theQueue.dequeue(); theQueue.dequeue(); theQueue.enqueue(50); theQueue.enqueue(60); // (wraps around) theQueue.enqueue(70); theQueue.enqueue(80); while (!theQueue.isEmpty()) { int n = theQueue.dequeue(); System.out.print(n); System.out.print(" "); } System.out.println(""); } }