Homework 9: Deque

The goal of this homework is to implement the “deque” data structure. You will practice:

Instructions

We encourage you to work with a partner.

Task 1: Reading

Ensure you are familiar with the material we covered in lecture about:

If not, review the recommended readings, especially Algorithms, chapter 1.3.

Task 2: Download Starter Files

deque.zip contains the starter files for this assignment.

Task 3: Implement Deque.java

A “deque” (pronounced “deck”) is a double-ended queue. A deque is a generalization of a stack and a queue that supports adding and removing items from either the front or the back of the data structure. You should create a generic data type Deque that implements the following API:

public class Deque<Item> implements Iterable<Item> {

    // construct an empty deque
    public Deque()

    // is the deque empty?
    public boolean isEmpty()

    // return the number of items on the deque
    public int size()

    // add the item to the front
    public void addFirst(Item item)

    // add the item to the back
    public void addLast(Item item)

    // remove and return the item from the front
    public Item removeFirst()

    // remove and return the item from the back
    public Item removeLast()

    // return an iterator over items in order from front to back
    public Iterator<Item> iterator()

    // unit testing (required)
    public static void main(String[] args)

}

Java contains data structures that support these operations (e.g., LinkedList, ArrayList). For this assignment, you cannot use these data structures, or the assignment will be far too easy. You should only import two classes: java.util.Iterator and java.util.NoSuchElementException. You will not receive credit for a submission that uses a class like the Java Standard Library’s LinkedList. Instead, you should use a nested Node class, similar to the TSP assignment.

Exceptions

Throw the specified exceptions for the following corner cases:

Hint: Use this syntax to throw an exception:

throw new IllegalArgumentException("A message describing the problem");

Of course, you should surround the exception with appropriate conditional logic, so it is only thrown when there is an actual problem.

Testing

We recommend writing JUnit tests to check every public method. Alternatively, you can add code to your main() method to verify that methods work as prescribed (e.g., by printing results to standard output).

Performance requirements

Your implementation must achieve the following worst-case performance requirements:

In summary:

Operation Big Theta
Non-iterator operations \(\Theta(1)\)
Iterator constructor \(\Theta(1)\)
Other iterator operations \(\Theta(1)\)


Memory Usage Big Theta
Deque \(\Theta(n)\)
Iterator \(\Theta(1)\)

Submit

Upload the following files to Gradescope:

Also, be sure to indicate your group member on Gradescope.

Acknowledgements

Thanks to Princeton’s COS126 materials, which served as a basis for this assignment.