You may use the textbook, your lecture notes, in-class examples, and other materials for this class. You may use any Java textbook. General online Java resources may be used with preapproval only (send me an e-mail if you plan to use any). You may not google for any problem-specific keywords.
The work is strictly individual. You may ask me questions in person or by e-mail, you may not ask anyone else any questions about any material on the test. Any exceptions to this rule (such as discussions with a TA) will be announced in class.
Please include your name in comments in each file and comment the program appropriately.
A doubly linked list is a linked list in which a node has a link not
just to the next node but also to the previous one (called
previous
). In addition to the firstNode
reference,
a doubly linked list has
a reference lastNode
to the last node.
The list has the flexibility of
being able to be traversed forward and backward. It cuts down
traversal time by accessing elements close to the end of the list
by
starting at the last node and traversing the previous references.
You will also need to write an iterator that traverses the list
backward, starting at the last node and following the previous
references. You need to change get
method so that it
traverses the list backward if the element is in the second half.
The purpose of the iterator is to check that references to the
previous node are correct.
Write tests for each of your methods before you write the actual method, this will help you better understand the intended behavior. Feel free to use JUnit if you so prefer.
Write an implementation of a doubly linked list
called DoublyLinkedList
. Use OurLinkedList
as a starting point (make sure to rename all necessary elements
appropriately).
Just
like OurLinkedList<E>
, it
implements OurList<E>
interface, and via it Iterable<E>
.
Please follow this sequence of steps below, starting at the
class that's
identical to the final version of OurLinkedList
class. This sequence of steps allows you to develop incrementally,
testing after each step. A different order would make incremental
development difficult.
previous
variable to
the Node
inner class.
lastNode
reference to the linked list class.
add
and remove
methods
so that they correctly set not just
the next references but also the previous ones. Be especially careful
with adding to an empty list (both firstNode and lastNode need to be
set) and
removing the last element from the list (both firstNode and lastNode must
become null).
LinkedListBackwardIterator
that implements
Iterator<E>
.
It starts at the last element and iterates backward traversing the
previous references. The iterator must not use get
.
private boolean iterateBackward = false;
to the DoublyLinkedList class and write a method
public void iterateBackward(boolean back)
that sets iterateBackward
to back
. If back
is true then the backward
iterator is used, otherwise the forward one (the one we developed in
class) is used.
iterator
method to return the usual iterator
when iterateBackward
is false
and LinkedListBackwardIterator<E>
when the variable
is true.
previous
references by setting iterateBackward
to true and running
a for-each
loop. It should print the list backward. If it doesn't, a mistake can
be in the iterator or in add/remove methods. Fix the errors (if any);
test with both forward and backward iterators.
get
method so that when the element is in
the second half of the linked list, you access it from the last
element going backward. Test your get method.
Please submit it by e-mail to me with the subject 2101 Midterm II.