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;
	int c = noElms;
	while (c!=0){
	    System.out.print(val[i]+" ");
	    i = (i+1) % val.length;
	    c--;
	}
    }

    public static void main(String[] args){
	Queue q = new Queue(10);
	for (int i=1; i<5; i++) q.insert(i);
	q.print();
    }
}


