public class ArrayQueue implements QueueInterface { private static final int CAPACITY = 1000; E[] queueArray; int front =0; int size=0; @Override public int size() { return size; } @Override public boolean isEmpty(){ return size==0; } @Override public E first() { if (isEmpty()) return null; return queueArray[front]; } @Override public void enqueue(E e) throws IllegalStateException { if (size==queueArray.length) throw new IllegalStateException("Queue full"); queueArray[(front+size)%queueArray.length] = e; size++; } @Override public E dequeue() { if (isEmpty()) return null; E e = queueArray[front]; queueArray[front] = null; front = (front+1)%queueArray.length; size--; return e; } public ArrayQueue() { this(CAPACITY); } @SuppressWarnings("unchecked") public ArrayQueue(int capacity) { queueArray = (E[])new Object[capacity]; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append("Siz:"); sb.append(size); sb.append(" Fnt:"); sb.append(front); sb.append(" ("); for (int i=front; i<(front+size); i++) { sb.append(queueArray[i]); if (i<(front+size-1)) sb.append(", "); } sb.append(")"); return sb.toString(); } public static void main(String[] args) { ArrayQueue qqq = new ArrayQueue<>(); qqq.enqueue(2); System.out.println(qqq); qqq.enqueue(3); System.out.println(qqq); qqq.dequeue(); System.out.println(qqq); qqq.enqueue(7); System.out.println(qqq); qqq.dequeue(); System.out.println(qqq); System.out.println("First:"+qqq.first()); qqq.dequeue(); System.out.println(qqq); qqq.dequeue(); System.out.println("Empty: " + qqq.isEmpty()); qqq.enqueue(9); System.out.println(qqq); qqq.enqueue(7); System.out.println(qqq); System.out.println("Size: " + qqq.size()); qqq.enqueue(3); System.out.println(qqq); qqq.enqueue(5); System.out.println(qqq); qqq.dequeue(); System.out.println(qqq); } }