Description: An array-based Queue class. Since this class is designed using an array the size of the Queue is limited to whatever the program gives you. MAX_SIZE
is made public to let a user know about MAX_SIZE
.
In Constructor, frontIndex
and rearIndex
set to 0. A point can be raised what if front()
or remove()
gets called on empty Queue then Integer.MIN_VALUE
is returned and size would still be 0.
package queue.arrayQueue;
public class ArrayQueue
{
public static final int MAX_SIZE = 1040;
private int frontIndex;
private int rearIndex;
private int queueElement[];
public ArrayQueue()
{
frontIndex = 0;
rearIndex = 0;
queueElement = new int [MAX_SIZE];
}
public int size()
{
return (MAX_SIZE + rearIndex - frontIndex) % MAX_SIZE;
}
public boolean isFull()
{
return (size() == MAX_SIZE - 1);
}
public boolean isEmpty()
{
return (size() == 0);
}
public void enqueue(int value)
{
if(!isFull())
{
queueElement[rearIndex] = value;
rearIndex = (rearIndex + 1) % MAX_SIZE;
}
}
public int front()
{
if(!isEmpty())
{
return queueElement[frontIndex];
}
return Integer.MIN_VALUE;
}
public void dequeue()
{
if (!isEmpty())
{
frontIndex = (frontIndex + 1) % MAX_SIZE;
}
}
}
remove()
in the code. did you meandequeue()
? \$\endgroup\$isFull()
assize() == MAX_SIZE - 1
rather thansize() == MAX_SIZE
? I would expectMAX_SIZE
to be the maximum amount of enqueued elements. \$\endgroup\$