class Queue{
int front, rear;
int capacity;
int[] array;
public Queue(int size) {
this.capacity=size;
this.front=-1;
this.rear=-1;
array=new int[this.capacity];
}
public boolean isEmptyQueue(){
return this.front==-1?true:false;
}
public boolean isFullQueue(){
return (this.rear+1)%this.capacity==this.front?true:false;
}
public int queueSize() {
if(this.rear==-1)return 0;
int k=0;
int p=this.front;
while((p++)%this.capacity!=this.rear){
k++;
};
return k+1;
}
public void enQueue(int data) {
if(isFullQueue()) {
System.out.println("Queue Overflow");
}else {
System.out.println(data+" is Enqueued");
this.rear=(this.rear+1)%this.capacity;
this.array[this.rear]=data;
if(this.front==-1) {
this.front=this.rear;
}
}
}
public int deQueue() {
int data=0;
if(isEmptyQueue()) {
System.out.println("Queue is Empty");
}else {
data=this.array[this.front];
System.out.println(data+" is Dequeued");
if(this.front==this.rear) {
this.front=-1;
this.rear=-1;
}else {
this.front=(this.front+1)%this.capacity;
}
}
return data;
}
}
public class QueueTest {
public static void main(String[] args) {
Queue Q=new Queue(3);
Q.enQueue(1);
Q.enQueue(2);
Q.enQueue(3);
Q.enQueue(4);
Q.deQueue();
Q.deQueue();
Q.deQueue();
Q.deQueue();
//Q.enQueue(4);
//Q.enQueue(5);
System.out.println(Q.queueSize());
}
}