Previous Fields

Posts I've Made

First off, I know this is a late reply, but this post came up right away when I did a Google search for ArrayQueue implementations.
I just started learning data structures in Java myself and found this quite helpful. I would like to add an extra feature to your queue, resizing. I find that this would be a better alternative to simply throwing a FullQueueException when enqueue-ing an item.

Here is the portion of code I am referring to

//Enqueue a new item in the array queue
public void enqueue(E newItem) {
//Check if the ArrayQueue is full
if(this.isFull())
throw new FullQueueException("Cannot enqueue, ArrayQueue is full");
...
}

Resizing can be tricky though since the queue array is circular. This is best explained through example:

Say our array is size 5 and our front and rear are 3,2 respectively:

0 1 2 3 4
[10][20][40][60][90]

Now, if we were to resize our queue to add another element our desired array would be

0 1 2 3 4 5
[60][90][10][20][40][newElement] , this way we keep the circular order of elements.

Here is a solution that is actually quite simple, but requires some tricky organization. The idea is to create two variables, one that will loop over a temporary array and another looping over the circular array. We then reassign the new array to reference the old array.

//Enqueue a new item in the array queue
public void enqueue(E newItem) {
//Check if the ArrayQueue is full
if(this.isFull()) resize(elements.length * 2); //I double the array. Really depends on situation though.
...
}

Thanks for your post, I know how much effort you put into it and want to thank you for that.