package DataStructures;
public class QueueArray {
static final int DEFAULT_CAPACITY = 10;
private Object[] theArray;
private int currentSize;
private int front;
private int back;
/**
* Construct the queue.
*
*/
public QueueArray() {
this(DEFAULT_CAPACITY);
}
/**
* Construct the queue.
*
* @param capacity
*/
public QueueArray(int capacity) {
theArray = new Object[capacity];
MakeEmpty();
}
/**
* Make the queue logically empty.
*
*/
public void MakeEmpty() {
currentSize = 0;
front = 0;
back = -1;
}
public boolean IsEmpty() {
return currentSize == 0;
}
public boolean IsFull() {
return currentSize == theArray.length;
}
/**
* Get the least recently inserted item in the queue. Does not alter the
* queue.
*
* @return the least recently inserted item in the queue, or null, if empty.
*/
public Object getFront() {
if (IsEmpty())
return null;
return theArray[front];
}
/**
* Insert a new item into the queue.
*
* @param x
* the item to insert.
* @throws Overflow
* if queue is full.
*/
public void Enqueue(Object x) throws Overflow {
if (IsFull())
throw new Overflow();
back = increment(back);
theArray[back] = x;
currentSize++;
}
/**
* Internal method to increment with wraparound.
*
* @param x
* any index in theArray`s range.
* @return x+1,or 0,if x is at the end of theArray;
*/
private int increment(int x) {
if (++x == theArray.length)
x = 0;
return x;
}
/**
* Return and remove the least recently inserted item from the queue.
*
* @return the least recently inserted item, or null, if empty.
*/
public Object Dequeue() {
if (IsEmpty())
return null;
currentSize--;
Object frontItem = theArray[front];
theArray[front] = null;
front = increment(front);
return frontItem;
}
}
public class QueueArray {
static final int DEFAULT_CAPACITY = 10;
private Object[] theArray;
private int currentSize;
private int front;
private int back;
/**
* Construct the queue.
*
*/
public QueueArray() {
this(DEFAULT_CAPACITY);
}
/**
* Construct the queue.
*
* @param capacity
*/
public QueueArray(int capacity) {
theArray = new Object[capacity];
MakeEmpty();
}
/**
* Make the queue logically empty.
*
*/
public void MakeEmpty() {
currentSize = 0;
front = 0;
back = -1;
}
public boolean IsEmpty() {
return currentSize == 0;
}
public boolean IsFull() {
return currentSize == theArray.length;
}
/**
* Get the least recently inserted item in the queue. Does not alter the
* queue.
*
* @return the least recently inserted item in the queue, or null, if empty.
*/
public Object getFront() {
if (IsEmpty())
return null;
return theArray[front];
}
/**
* Insert a new item into the queue.
*
* @param x
* the item to insert.
* @throws Overflow
* if queue is full.
*/
public void Enqueue(Object x) throws Overflow {
if (IsFull())
throw new Overflow();
back = increment(back);
theArray[back] = x;
currentSize++;
}
/**
* Internal method to increment with wraparound.
*
* @param x
* any index in theArray`s range.
* @return x+1,or 0,if x is at the end of theArray;
*/
private int increment(int x) {
if (++x == theArray.length)
x = 0;
return x;
}
/**
* Return and remove the least recently inserted item from the queue.
*
* @return the least recently inserted item, or null, if empty.
*/
public Object Dequeue() {
if (IsEmpty())
return null;
currentSize--;
Object frontItem = theArray[front];
theArray[front] = null;
front = increment(front);
return frontItem;
}
}