You are on page 1of 2

/**

* @(#)Queue.java
*
*
* @author
* @version 1.00 2015/3/2
*/

public class CircularQueue {

private int front;


private int tail;
private int size;
private int counter;
private int[] queue;

//This is a constructor to construct a circular queue object


public CircularQueue()
{
front=0;
tail=-1;
counter=0; //keep the number of item in the circular queue
size=5;

queue = new int[size]; // create circular queue object


}

//This method is to add item into the circular queue object


public void enqueue(int item)
{
if (tail == size-1) // if tail reaches max index
tail=0;
else
tail++;

queue[tail] = item;

counter++;
}

//This method is to remove item from the circular queue object


public int dequeue()
{ int temp;

temp = queue[front];

if (front == size-1) // if front reaches max index


front = 0;
else
front++;

counter--;

return temp;
}

//This method is to check whether the circular queue is empty or not


public boolean isEmpty()
{
if (counter == 0)
return true; // if circular queue is empty
else
return false; // if circular queue is not empty
}

//This method is to check whether the circular queue is full or not


public boolean isFull()
{
if (counter == size)
return true; // if circular queue is full
else
return false; // if circular queue is not full
}

} //end class

You might also like