ResizableArray based queue implementation

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I am implementing basic data structures in Java. I have written this simple resizing array queue implementation and looking for ways to simplify the implementation as it looks like I am using too many variables which can be reduced. I would appreciate any ideas to make it simpler, implementation advice and constructive criticisms.
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayQueue<T> implements Iterable<T>
private T mQueue;
private int mHead, mTail, mCurrentSize, mSize;
public Iterator<T> iterator()
return new QueueIterator();
public ResizingArrayQueue()
mQueue = (T) new Object[1];
mHead = 0;
mSize = 1;
mTail = mSize - 1;
mCurrentSize = 0;
public void enQueue(T valueToEnque)
if (mCurrentSize == mSize)
resize(2 * mSize);
mTail = ++mTail % mSize;
mQueue[mTail] = valueToEnque;
mCurrentSize++;
public T deQueue()
if(mCurrentSize == mSize / 4)
resize(mSize / 2);
T value = mQueue[mHead % mSize];
mQueue[mHead % mSize] = null;
mHead++;
mCurrentSize--;
return value;
public boolean isEmpty()
return mCurrentSize == 0;
public void resize(int size)
T newQueue = (T) new Object[size];
int mCurrent = 0;
while(mCurrent < mCurrentSize)
newQueue[mCurrent] = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
mQueue = newQueue;
mSize = size;
mHead = 0;
mTail = mCurrentSize - 1;
private class QueueIterator implements Iterator<T>
private int mCurrent = 0;
public boolean hasNext()
return mCurrent < mCurrentSize;
public T next()
if (!hasNext())
throw new NoSuchElementException();
T valueToReturn = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
return valueToReturn;
public void remove()
throw new UnsupportedOperationException();
java queue
add a comment |Â
up vote
1
down vote
favorite
I am implementing basic data structures in Java. I have written this simple resizing array queue implementation and looking for ways to simplify the implementation as it looks like I am using too many variables which can be reduced. I would appreciate any ideas to make it simpler, implementation advice and constructive criticisms.
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayQueue<T> implements Iterable<T>
private T mQueue;
private int mHead, mTail, mCurrentSize, mSize;
public Iterator<T> iterator()
return new QueueIterator();
public ResizingArrayQueue()
mQueue = (T) new Object[1];
mHead = 0;
mSize = 1;
mTail = mSize - 1;
mCurrentSize = 0;
public void enQueue(T valueToEnque)
if (mCurrentSize == mSize)
resize(2 * mSize);
mTail = ++mTail % mSize;
mQueue[mTail] = valueToEnque;
mCurrentSize++;
public T deQueue()
if(mCurrentSize == mSize / 4)
resize(mSize / 2);
T value = mQueue[mHead % mSize];
mQueue[mHead % mSize] = null;
mHead++;
mCurrentSize--;
return value;
public boolean isEmpty()
return mCurrentSize == 0;
public void resize(int size)
T newQueue = (T) new Object[size];
int mCurrent = 0;
while(mCurrent < mCurrentSize)
newQueue[mCurrent] = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
mQueue = newQueue;
mSize = size;
mHead = 0;
mTail = mCurrentSize - 1;
private class QueueIterator implements Iterator<T>
private int mCurrent = 0;
public boolean hasNext()
return mCurrent < mCurrentSize;
public T next()
if (!hasNext())
throw new NoSuchElementException();
T valueToReturn = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
return valueToReturn;
public void remove()
throw new UnsupportedOperationException();
java queue
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am implementing basic data structures in Java. I have written this simple resizing array queue implementation and looking for ways to simplify the implementation as it looks like I am using too many variables which can be reduced. I would appreciate any ideas to make it simpler, implementation advice and constructive criticisms.
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayQueue<T> implements Iterable<T>
private T mQueue;
private int mHead, mTail, mCurrentSize, mSize;
public Iterator<T> iterator()
return new QueueIterator();
public ResizingArrayQueue()
mQueue = (T) new Object[1];
mHead = 0;
mSize = 1;
mTail = mSize - 1;
mCurrentSize = 0;
public void enQueue(T valueToEnque)
if (mCurrentSize == mSize)
resize(2 * mSize);
mTail = ++mTail % mSize;
mQueue[mTail] = valueToEnque;
mCurrentSize++;
public T deQueue()
if(mCurrentSize == mSize / 4)
resize(mSize / 2);
T value = mQueue[mHead % mSize];
mQueue[mHead % mSize] = null;
mHead++;
mCurrentSize--;
return value;
public boolean isEmpty()
return mCurrentSize == 0;
public void resize(int size)
T newQueue = (T) new Object[size];
int mCurrent = 0;
while(mCurrent < mCurrentSize)
newQueue[mCurrent] = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
mQueue = newQueue;
mSize = size;
mHead = 0;
mTail = mCurrentSize - 1;
private class QueueIterator implements Iterator<T>
private int mCurrent = 0;
public boolean hasNext()
return mCurrent < mCurrentSize;
public T next()
if (!hasNext())
throw new NoSuchElementException();
T valueToReturn = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
return valueToReturn;
public void remove()
throw new UnsupportedOperationException();
java queue
I am implementing basic data structures in Java. I have written this simple resizing array queue implementation and looking for ways to simplify the implementation as it looks like I am using too many variables which can be reduced. I would appreciate any ideas to make it simpler, implementation advice and constructive criticisms.
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayQueue<T> implements Iterable<T>
private T mQueue;
private int mHead, mTail, mCurrentSize, mSize;
public Iterator<T> iterator()
return new QueueIterator();
public ResizingArrayQueue()
mQueue = (T) new Object[1];
mHead = 0;
mSize = 1;
mTail = mSize - 1;
mCurrentSize = 0;
public void enQueue(T valueToEnque)
if (mCurrentSize == mSize)
resize(2 * mSize);
mTail = ++mTail % mSize;
mQueue[mTail] = valueToEnque;
mCurrentSize++;
public T deQueue()
if(mCurrentSize == mSize / 4)
resize(mSize / 2);
T value = mQueue[mHead % mSize];
mQueue[mHead % mSize] = null;
mHead++;
mCurrentSize--;
return value;
public boolean isEmpty()
return mCurrentSize == 0;
public void resize(int size)
T newQueue = (T) new Object[size];
int mCurrent = 0;
while(mCurrent < mCurrentSize)
newQueue[mCurrent] = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
mQueue = newQueue;
mSize = size;
mHead = 0;
mTail = mCurrentSize - 1;
private class QueueIterator implements Iterator<T>
private int mCurrent = 0;
public boolean hasNext()
return mCurrent < mCurrentSize;
public T next()
if (!hasNext())
throw new NoSuchElementException();
T valueToReturn = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
return valueToReturn;
public void remove()
throw new UnsupportedOperationException();
java queue
edited Mar 4 at 3:40
Jamalâ¦
30.1k11114225
30.1k11114225
asked Mar 4 at 3:33
Anurag Arora
82
82
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
private T mQueue;
private int mHead, mTail, mCurrentSize, mSize;
The value that you are storing in mSize is available as mQueue.length.
private T mQueue = (T) new Object[1];
private int mHead = 0;
private int mSize = 0;
But if you get rid of mSize, then there is no reason to differentiate mCurrentSize. So just call that mSize.
You also don't need mTail, as mHead, mSize, and mQueue.length determine mTail uniquely.
You don't need a constructor, as you can initialize these values without further calculation.
T newQueue = (T) new Object[size];
int mCurrent = 0;
while(mCurrent < mCurrentSize)
newQueue[mCurrent] = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
mQueue = newQueue;
mSize = size;
mHead = 0;
mTail = mCurrentSize - 1;
There's a built-in for this.
mQueue = Arrays.copyOfRange(mQueue, mHead, mHead + size);
mHead = 0;
Now you don't newQueue or mCurrent.
As previously said, you don't need mSize or mTail.
public void enQueue(T valueToEnque)
if (mCurrentSize == mSize)
resize(2 * mSize);
mTail = ++mTail % mSize;
mQueue[mTail] = valueToEnque;
mCurrentSize++;
public T deQueue()
if(mCurrentSize == mSize / 4)
resize(mSize / 2);
T value = mQueue[mHead % mSize];
mQueue[mHead % mSize] = null;
mHead++;
mCurrentSize--;
return value;
To implement, you could do it like
public void enQueue(T valueToEnque)
if (mSize >= mQueue.length)
resize(2 * mQueue.length);
mQueue[(mHead + mSize) % mQueue.length] = valueToEnque;
mSize++;
public T deQueue()
if (mSize <= mQueue.length / 4)
resize(mQueue.length / 2);
T value = mQueue[mHead];
mQueue[mHead] = null;
mHead = ++mHead % mQueue.length;
mSize--;
return value;
By using <= or >= rather than strict equality, we can catch situations where the size jumped over the gating. For example, if you implemented QueueIterator.remove without calling deQueue, you might find that you had skipped over the resize check.
See how we determine the value that would have been held in mTail as needed rather than maintaining it.
Notice how things are easier if we maintain mHead as the actual index rather than allowing it to grow unbounded. Note that this also avoids integer overflow in an edge case (if you never resize, mHead always grows).
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
private T mQueue;
private int mHead, mTail, mCurrentSize, mSize;
The value that you are storing in mSize is available as mQueue.length.
private T mQueue = (T) new Object[1];
private int mHead = 0;
private int mSize = 0;
But if you get rid of mSize, then there is no reason to differentiate mCurrentSize. So just call that mSize.
You also don't need mTail, as mHead, mSize, and mQueue.length determine mTail uniquely.
You don't need a constructor, as you can initialize these values without further calculation.
T newQueue = (T) new Object[size];
int mCurrent = 0;
while(mCurrent < mCurrentSize)
newQueue[mCurrent] = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
mQueue = newQueue;
mSize = size;
mHead = 0;
mTail = mCurrentSize - 1;
There's a built-in for this.
mQueue = Arrays.copyOfRange(mQueue, mHead, mHead + size);
mHead = 0;
Now you don't newQueue or mCurrent.
As previously said, you don't need mSize or mTail.
public void enQueue(T valueToEnque)
if (mCurrentSize == mSize)
resize(2 * mSize);
mTail = ++mTail % mSize;
mQueue[mTail] = valueToEnque;
mCurrentSize++;
public T deQueue()
if(mCurrentSize == mSize / 4)
resize(mSize / 2);
T value = mQueue[mHead % mSize];
mQueue[mHead % mSize] = null;
mHead++;
mCurrentSize--;
return value;
To implement, you could do it like
public void enQueue(T valueToEnque)
if (mSize >= mQueue.length)
resize(2 * mQueue.length);
mQueue[(mHead + mSize) % mQueue.length] = valueToEnque;
mSize++;
public T deQueue()
if (mSize <= mQueue.length / 4)
resize(mQueue.length / 2);
T value = mQueue[mHead];
mQueue[mHead] = null;
mHead = ++mHead % mQueue.length;
mSize--;
return value;
By using <= or >= rather than strict equality, we can catch situations where the size jumped over the gating. For example, if you implemented QueueIterator.remove without calling deQueue, you might find that you had skipped over the resize check.
See how we determine the value that would have been held in mTail as needed rather than maintaining it.
Notice how things are easier if we maintain mHead as the actual index rather than allowing it to grow unbounded. Note that this also avoids integer overflow in an edge case (if you never resize, mHead always grows).
add a comment |Â
up vote
0
down vote
accepted
private T mQueue;
private int mHead, mTail, mCurrentSize, mSize;
The value that you are storing in mSize is available as mQueue.length.
private T mQueue = (T) new Object[1];
private int mHead = 0;
private int mSize = 0;
But if you get rid of mSize, then there is no reason to differentiate mCurrentSize. So just call that mSize.
You also don't need mTail, as mHead, mSize, and mQueue.length determine mTail uniquely.
You don't need a constructor, as you can initialize these values without further calculation.
T newQueue = (T) new Object[size];
int mCurrent = 0;
while(mCurrent < mCurrentSize)
newQueue[mCurrent] = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
mQueue = newQueue;
mSize = size;
mHead = 0;
mTail = mCurrentSize - 1;
There's a built-in for this.
mQueue = Arrays.copyOfRange(mQueue, mHead, mHead + size);
mHead = 0;
Now you don't newQueue or mCurrent.
As previously said, you don't need mSize or mTail.
public void enQueue(T valueToEnque)
if (mCurrentSize == mSize)
resize(2 * mSize);
mTail = ++mTail % mSize;
mQueue[mTail] = valueToEnque;
mCurrentSize++;
public T deQueue()
if(mCurrentSize == mSize / 4)
resize(mSize / 2);
T value = mQueue[mHead % mSize];
mQueue[mHead % mSize] = null;
mHead++;
mCurrentSize--;
return value;
To implement, you could do it like
public void enQueue(T valueToEnque)
if (mSize >= mQueue.length)
resize(2 * mQueue.length);
mQueue[(mHead + mSize) % mQueue.length] = valueToEnque;
mSize++;
public T deQueue()
if (mSize <= mQueue.length / 4)
resize(mQueue.length / 2);
T value = mQueue[mHead];
mQueue[mHead] = null;
mHead = ++mHead % mQueue.length;
mSize--;
return value;
By using <= or >= rather than strict equality, we can catch situations where the size jumped over the gating. For example, if you implemented QueueIterator.remove without calling deQueue, you might find that you had skipped over the resize check.
See how we determine the value that would have been held in mTail as needed rather than maintaining it.
Notice how things are easier if we maintain mHead as the actual index rather than allowing it to grow unbounded. Note that this also avoids integer overflow in an edge case (if you never resize, mHead always grows).
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
private T mQueue;
private int mHead, mTail, mCurrentSize, mSize;
The value that you are storing in mSize is available as mQueue.length.
private T mQueue = (T) new Object[1];
private int mHead = 0;
private int mSize = 0;
But if you get rid of mSize, then there is no reason to differentiate mCurrentSize. So just call that mSize.
You also don't need mTail, as mHead, mSize, and mQueue.length determine mTail uniquely.
You don't need a constructor, as you can initialize these values without further calculation.
T newQueue = (T) new Object[size];
int mCurrent = 0;
while(mCurrent < mCurrentSize)
newQueue[mCurrent] = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
mQueue = newQueue;
mSize = size;
mHead = 0;
mTail = mCurrentSize - 1;
There's a built-in for this.
mQueue = Arrays.copyOfRange(mQueue, mHead, mHead + size);
mHead = 0;
Now you don't newQueue or mCurrent.
As previously said, you don't need mSize or mTail.
public void enQueue(T valueToEnque)
if (mCurrentSize == mSize)
resize(2 * mSize);
mTail = ++mTail % mSize;
mQueue[mTail] = valueToEnque;
mCurrentSize++;
public T deQueue()
if(mCurrentSize == mSize / 4)
resize(mSize / 2);
T value = mQueue[mHead % mSize];
mQueue[mHead % mSize] = null;
mHead++;
mCurrentSize--;
return value;
To implement, you could do it like
public void enQueue(T valueToEnque)
if (mSize >= mQueue.length)
resize(2 * mQueue.length);
mQueue[(mHead + mSize) % mQueue.length] = valueToEnque;
mSize++;
public T deQueue()
if (mSize <= mQueue.length / 4)
resize(mQueue.length / 2);
T value = mQueue[mHead];
mQueue[mHead] = null;
mHead = ++mHead % mQueue.length;
mSize--;
return value;
By using <= or >= rather than strict equality, we can catch situations where the size jumped over the gating. For example, if you implemented QueueIterator.remove without calling deQueue, you might find that you had skipped over the resize check.
See how we determine the value that would have been held in mTail as needed rather than maintaining it.
Notice how things are easier if we maintain mHead as the actual index rather than allowing it to grow unbounded. Note that this also avoids integer overflow in an edge case (if you never resize, mHead always grows).
private T mQueue;
private int mHead, mTail, mCurrentSize, mSize;
The value that you are storing in mSize is available as mQueue.length.
private T mQueue = (T) new Object[1];
private int mHead = 0;
private int mSize = 0;
But if you get rid of mSize, then there is no reason to differentiate mCurrentSize. So just call that mSize.
You also don't need mTail, as mHead, mSize, and mQueue.length determine mTail uniquely.
You don't need a constructor, as you can initialize these values without further calculation.
T newQueue = (T) new Object[size];
int mCurrent = 0;
while(mCurrent < mCurrentSize)
newQueue[mCurrent] = mQueue[(mCurrent + mHead) % mSize];
mCurrent++;
mQueue = newQueue;
mSize = size;
mHead = 0;
mTail = mCurrentSize - 1;
There's a built-in for this.
mQueue = Arrays.copyOfRange(mQueue, mHead, mHead + size);
mHead = 0;
Now you don't newQueue or mCurrent.
As previously said, you don't need mSize or mTail.
public void enQueue(T valueToEnque)
if (mCurrentSize == mSize)
resize(2 * mSize);
mTail = ++mTail % mSize;
mQueue[mTail] = valueToEnque;
mCurrentSize++;
public T deQueue()
if(mCurrentSize == mSize / 4)
resize(mSize / 2);
T value = mQueue[mHead % mSize];
mQueue[mHead % mSize] = null;
mHead++;
mCurrentSize--;
return value;
To implement, you could do it like
public void enQueue(T valueToEnque)
if (mSize >= mQueue.length)
resize(2 * mQueue.length);
mQueue[(mHead + mSize) % mQueue.length] = valueToEnque;
mSize++;
public T deQueue()
if (mSize <= mQueue.length / 4)
resize(mQueue.length / 2);
T value = mQueue[mHead];
mQueue[mHead] = null;
mHead = ++mHead % mQueue.length;
mSize--;
return value;
By using <= or >= rather than strict equality, we can catch situations where the size jumped over the gating. For example, if you implemented QueueIterator.remove without calling deQueue, you might find that you had skipped over the resize check.
See how we determine the value that would have been held in mTail as needed rather than maintaining it.
Notice how things are easier if we maintain mHead as the actual index rather than allowing it to grow unbounded. Note that this also avoids integer overflow in an edge case (if you never resize, mHead always grows).
answered Mar 4 at 11:08
mdfst13
16.8k42055
16.8k42055
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188769%2fresizablearray-based-queue-implementation%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password