class Queue { int[] val; int front, rear, noElms; public Queue(int size){ val = new int[size]; front = 0; rear = -1; noElms = 0; } boolean isEmpty(){ return noElms==0; } boolean isFull(){ return noElms==val.length; } public void insert(int x){ if (isFull()){ throw new RuntimeException("Queue overflow"); } else { rear = (rear+1) % val.length; val[rear] = x; noElms++; } } public int remove(){ if (isEmpty()){ throw new RuntimeException("Queue underflow"); } else { int x = val[front]; front = (front+1) % val.length; noElms--; return x; } } public void print(){ int i = front; if (noElms==0) return; while (i != rear){ System.out.print(val[i]+" "); i = (i+1) % val.length; } System.out.println(); } public static void main(String[] args){ Queue q = new Queue(5); for (int i=1; i<10; i++) q.insert(i); q.print(); } }