| CSC 207 | Algorithms and Object Oriented Design | Spring 2010 |
Summary: In this lab, you will practice implementing (and testing!) your own specific collection. Namely, a double-ended queue (aka Dequeue).
mkdir somewhere/dequeue
cd somewhere/dequeue
cp ~weinman/public_html/courses/CSC207/2010S/labs/code/dequeue/*.java .This should place a class skeleton,
Dequeue.java, a
test suite, TestDequeue.java into your
directory . (Don't worry if cp complains that it can't
copy my solution file Dequeue.SOL.java... that's the
idea.)
As you should know from your reading of Weiss 16.5, a double-ended queue allows access at "both ends." In other words, it acts like both a queue (add to end, retrieve from front), and a stack (add to front, retrieve from front). Plus, it allows you to retrieve from the end as well. Thus, it is like a mini-Swiss army knife of data structures. (A list would probably be the full-on Swiss army knife, since it allows arbitrary access.)
Our dequeue implementation will support six core operations,
void addFront( AnyType x ) // Insert x at front
void addRear( AnyType x ) // Insert x at rear
AnyType getFront() // Return item at front
AnyType getRear() // Return item at rear
AnyType removeFront() // Return and remove item at front
AnyType removeRear( ) // Return and remove item at rear
dq.addFront(3) dq.addRear(5) dq.addFront(7) dq.addFront(6) dq.removeRear() dq.removeRear() dq.removeFront() dq.removeRear()
Dequeue.java in Emacs.
DequeueisEmptymakeEmptygetFrontgetRearremoveFrontremoveRearaddFrontaddRearTestDequeue.java in Emacs. This
contains a unit test suite using a testing library. Briefly scan
the methods to see what is tested.
Dequeue.java
and TestDequeue.java from Emacs (remember the
useful short cut C-c C-v C-c).
Hey, at least 8 things worked, right?TestDequeue: --------------- . new TestDequeue:1() --------------- Found 17 test methods Ran 51 tests. 43 tests failed.
front, back,
and currentSize, what should the
method makeEmpty do? That is, will it differ from the
implementation in Figure 16.17 (p. 605)?
makeEmpty in Dequeue.java
isEmpty differ from
the implementation in Figure 16.13 (p. 603)?
isEmpty in Dequeue.java
getFront is akin to the same method in
Weiss's ArrayQueue class. Given your member
variables, will the implementation differ substantially?
getFront in Dequeue.java. Pay
careful attention to the specification for the exception (the
tester cares what the message is).
getRear is new. How will you implement it
using your member variables?
getRear in Dequeue.java. (Again, pay
attention to the specification for the exception.)
addRear is akin to enqueue in
the original ArrayQueue class. Will the
implementation for a dequeue differ substantially?
addRear and compile your code.
removeFront is akin
to dequeue in the original ArrayQueue
class. Will the implementation for your Dequeue class
differ substantially?
removeFront and compile your code.
The method addFront is new the dequeue structure. It is
essentially the "reverse" of addRear. The original
queue has a method increment to implement wrap-around
from the end of the array back to the beginning. In order to be
able to add to the front of the queue, we'll need to also
wrap-around from the beginning of the array back to the end.
increment and then review the
documentation/specification for decrement.
decrement and verify that your code compiles.
addRear, which you have already
implemented. As mentioned above, addFront will be
similar.
front change when you add an item?
back change when you add an item?
addFront based on your replies. (Don't
forget to use decrement. Of course, by now you should
be getting used to compiling your code frequently. So compile
already!
This should work correctly and return 1. Consider carefully the status ofdq.addFront(1) dq.getRear()
front, and especially back in
your code after the first operation.
removeRear method. It acts like the
"reverse" of the removeFront method.
front change?
back change?
removeRear based on your replies above.
weiss.nonstandard.ListNode to your
current directory and remove the package specification.
ListNode class to implement your own
list-backed version of Deque
called ListDeque.
TestDeque.java
to TestListDeque.java and change all the references
to Deque objects to ListDeque objects.
ListDeque and fix any bugs.