Welcome to the Deque Tester! Enter 1 to test an array-based deque; Enter 2 to test a Link2-based deque > 2 >h q : to quit h : for help ps : to print size of deque pr : to print item at rear of deque pf : to print item at front of deque pd : to print all items in deque rr : to remove item from rear rf : to remove item from front ir x : to insert string x at rear if x : to insert string x at front >ir the Inserting "the" at rear... Contents of deque: [ the ] >if in Inserting "in" at front... Contents of deque: [ in the ] >ps Size of deque is 2 >pd Contents of deque: [ in the ] >if cat Inserting "cat" at front... Contents of deque: [ cat in the ] >ps Size of deque is 3 >ir hat Inserting "hat" at rear... Contents of deque: [ cat in the hat ] >pf Front item is cat >pr Rear item is hat >if A Inserting "A" at front... Contents of deque: [ A cat in the hat ] >rf Removing item at front... Contents of deque: [ cat in the hat ] >ir croaked Inserting "croaked" at rear... Contents of deque: [ cat in the hat croaked ] >rr Removing item at rear... Contents of deque: [ cat in the hat ] >rf Removing item at front... Contents of deque: [ in the hat ] >rf Removing item at front... Contents of deque: [ the hat ] >rr Removing item at rear... Contents of deque: [ the ] >rf Removing item at front... Contents of deque: [ ] >q Goodbye. |
A double-ended queue (deque, for short) is a linear container structure, like a stack or a queue. In a stack, insertions and removals of elements occur at the same end (referred to as the top). In a queue, insertions occur at one end (the rear) and removals at the other (the front). A deque combines these capabilities by allowing both insertions and removals to occur at either end. (By convention, we refer to the two ends of a deque using the same terms as in a queue: front and rear.)
Array-based implementations of stacks and queues were covered in lecture. In particular, the array-based implementation of a queue imagined the array as being a "wraparound" or circular structure in which location zero "follows" location N−1 (i.e., the array's last location). (See the relevant links about queues on the CMPS 144 course web page.)
Similarly, reference-based representations of stacks and queues were covered in lecture. They used instances of the Link1 class to store the elements on the stack or queue. For this activity, you are to complete two Java classes, one of which implements deques using a wraparound array-based representation and the other of which uses a reference-based representation. You will need these files:
Your testing should be more thorough than what is shown there. In particular, you should make sure to test conditions involving an empty deque. That is, make sure that inserting (at both front and rear) works correctly when a deque is empty. Similarly, make sure that removing the last item from a deque (whether it be removed from the front or the rear) not only leaves you with an empty deque but also one on which a subsequent insertion (at front or rear) works correctly.
front rear +---+ +---+ | * | | * | +-+-+ +-+-+ | | | | V V +-----+ +-----+ +-----+ +-----+ +-----+ prev | null|<---+--* |<---+--* |<---|--* |<---+--* | prev +-----+ +-----+ +-----+ +-----+ +-----+ item | DOG | | CAT | | ELK | | BUG | | PIG | item +-----+ +-----+ +-----+ +-----+ +-----+ next | *--+--->| *--+--->| *--+--->| *--+--->| null| next +-----+ +-----+ +-----+ +-----+ +-----+ |