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;
}
}
}
1 Answer 1
It is good practise to define an interface (or use one that is already defined) and implement it in your class, especially for containers. That way a user can replace your implementation by another one (or vice versa) without changing any surrounding code.
public static final int MAX_SIZE = 1040; private int frontIndex; private int rearIndex; private int queueElement[];
I like to improve readability by separating groups of members with different access modifiers with a blank line in between.
int queueElement[]
is valid in Java, but the syntax stems from C. I would always use int[] queueElement
, which is more idomatic in Java. In my opinion it is also way more intuitive, because it reads like "an integer array named queueElement", rather than "an integer named queueElement array".
Since an array (usually) contains multiple elements, the convention is to use a plural name like queueElements
.
public ArrayQueue() { frontIndex = 0; rearIndex = 0; queueElement = new int [MAX_SIZE]; }
These member variables are already set to 0 by default. It is good to be explicit, but I would do it in the member declaration rather than the constructor to make the code shorter without losing readability.
public boolean isFull() { return (size() == MAX_SIZE - 1); }
Most users would probably expect MAX_SIZE
to be the maximum amount of enqueued items, rather than MAX_SIZE - 1
. I am not sure whether you thought about it as an array index, or whether this compensates for an off by one error in the size()
method. If a user checks for the MAX_SIZE and adds 1040 items in a loop, then, because enqueue()
neither throws an exception nor returns anything when the queue is full, the last item will just be ignored silently.
-
\$\begingroup\$ should the Interface be in the same package as the class? @Raimund \$\endgroup\$Hamidur Rahman– Hamidur Rahman2018年05月30日 17:43:17 +00:00Commented May 30, 2018 at 17:43
-
1\$\begingroup\$ Not necessarily, but since it is your default implementation of the interface, I would put it in the same package. \$\endgroup\$Raimund Krämer– Raimund Krämer2018年06月02日 13:29:18 +00:00Commented Jun 2, 2018 at 13:29
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\$