CS 240: Algorithms and Data Structures
James Madison University, Fall 2021

Design Issues with AQueue

There are some questionable design decisions in the way the author's of our textbook chose to implement their array-based queue:

  1. It probably isn't a good idea to maintain a separate maxSize variable to track the capacity of the queue. This information is already accessible as queueArray.length. Storing the same piece of information in two different places introduces unnecessary complexity and opens the door to bugs.

  2. The authors describe two possible approaches for representing the front and rear of the queue:

    "One obvious solution is to keep an explicit count of the number of elements in the queue [...]. Another solution is to make the array be of size n+1, and only allow n elements to be stored. Which of these solutions to adopt is purely a matter of the implementor's taste in such affairs. Our choice here is to use an array of size n+1."

    While it's true that either approach will work, maintaining an explicit count of the number of elements stored leads to a cleaner solution.

  3. Once again, their array-based implementation does not make use of dynamic arrays to enable arbitrarily large queues.

  4. The dequeue and clear methods interfere with garbage collection by maintaining unused references to elements that are no longer stored in the Queue.

Refactoring AQueue

Download the following Java files:

Complete the implementation of AQueue.java to address all four of the issues above. You are welcome to refer to the provided OpenDSA AQueue implementation, but keep in mind that most of your methods will look very different. For example, your length method should just return the value of a size variable. Use the provided unit tests to confirm that the behavior is correct.

Submitting

Submit AQueue.java through Autolab.