Linked Deque HW
The Java Collections Framework provides a Deque interface (pronounced like "deck", short for double-ended queue). Deques allow for insertion and removal at either end of the collection, and so can be used in place of either stacks or queues.
The Java Collections Framework provides a number of concrete classes that implement the Deque interface, including ArrayDeque and LinkedList. Your goal for this homework will be to provide an alternate implementation based on a doubly-linked list.
In this implementation, each node in the linked list will have a reference to the next node and the previous node. The deque itself will have references to the head (first) and tail (last) nodes. This will allow us to add and remove elements from both ends of the deque in constant time.
You will also be asked to write tests that both verify the correctness of your implementation and demonstrate your understanding of the deque's behavior.
Starter Code
Download the following files:
- AbstractDeque.java – (FINISHED) Abstract base class that simplifies Deque implementation.
- LinkedDeque.java – (UNFINISHED) This is the file you will be implementing.
- TestLinkedDeque.java – (UNFINISHED) This is the file you will be using to test your implementation.
Instructions
Implement each of the remaining methods in LinkedDeque.java. As you complete each method, write and run tests in TestLinkedDeque.java to verify that your implementation is correct. Make sure to also submit early and often to Gradescope to check your progress.
Read through the following sections to understand how the deque should behave and how the linked list should be implemented.
Visualizing the LinkedDeque
It's almost always helpful to draw a picture and visualize how the linked list works and how the references are updated.
Offering
Think about what happens when we offerLast. There are two cases to consider.
-
When the deque is empty:
Note that both
headandtailshould reference the new node. -
When there is at least one element in the deque:
In the second case, what references need to be updated?
Polling
Consider this LinkedDeque:
If we call pollFirst(), this should return 'S' and then the deque would look like this:
Take note of the differences between the two images. Specifically, what references need to be updated?
If we then call pollLast(), this should return 'T' and the deque would look like this:
One Element Left
Don't forget about the special case when the deque has only one element:
Here, you'll need to update both head and tail.
Hints
For poll methods, don't leave any references to the Node you remove!
- For example, if I had [A, B, C] and called
pollLast(),tailshould be pointing to node B, and node B's.nextshould be null. - Otherwise, the stale reference will prevent the garbage collector from recovering node C.
For the iterator's remove method, here are a few tips:
- Don't worry about
current. Thelastreference already points to the item you need to remove. Work withlast. - There are a few special cases to consider:
- What if
lastpoints to the first item in the list (aka head)? Do you have a method you can call that removes the first item? Call it. - What if
lastpoints to the last item in the list (aka tail)? Do you have a method you can call that removes the last item? Call it. - Otherwise, you are removing from the middle. You are guaranteed that
lastis surrounded by two other nodes. Think about the references you need to change here.
- What if
- After removing
last, don't hold a reference to it anymore! Setlastto null.
Submission and Grading
Submit LinkedDeque.java and TestLinkedDeque.java to
Gradescope. Your code must pass your own tests to receive credit.
Full credit for your unit tests will require 100% statement coverage
of LinkedDeque.java.
Gradescope will run additional tests on your code that are not
included in TestLinkedDeque.java. Style checks will be run on
LinkedDeque.java, but not on TestLinkedDeque.java.
The assignment will be graded as follows:
| Statement Coverage for Student-Developed Unit Tests | 40% |
| Instructor Gradescope Functionality Tests | 50% |
| Gradescope Style Checks | 10% |