ResizableArray based queue implementation

The name of the pictureThe name of the pictureThe name of the pictureClash 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();









share|improve this question



























    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();









    share|improve this question























      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();









      share|improve this question













      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();











      share|improve this question












      share|improve this question




      share|improve this question








      edited Mar 4 at 3:40









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Mar 4 at 3:33









      Anurag Arora

      82




      82




















          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).






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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).






            share|improve this answer

























              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).






              share|improve this answer























                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).






                share|improve this answer














                 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).







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Mar 4 at 11:08









                mdfst13

                16.8k42055




                16.8k42055






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    Python Lists

                    Aion

                    JavaScript Array Iteration Methods