A deque (which is derived from "doubled-ended queue") 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.)
Reference-based implementations of stacks and queues were covered in lecture, using the Link1 utility class. (See the relevant links on the CMPS 144 course web page.)
For this activity, you are to complete a reference-based implementation of deques. You will need these files:
item next item next item next item next +----+----+ +----+----+ +----+----+ +----+----+ | * | *-+---> | * | *-+---> | * | *-+---> | * | *-+---|| +--+-+----+ +--+-+----+ +--+-+----+ +--+-+----+ | | | | | | | | V V V V +---+ +---+ +---+ +---+ |DOG| |PIG| |CAT| |ELK| +---+ +---+ +---+ +---+ |
The instance variables are front, rear, and numItems. With respect to the figure above, imagine front and rear as being references/pointers to the leftmost and rightmost, respectively, Link1 nodes in the picture. Then, going from the front of the deque to its rear, the elements would be DOG, PIG, CAT, and ELK. Of course, with this example, the value of numItems would be 4.
Welcome to the Deque Tester! >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... Deque is now: [ the ] >if in Inserting "in" at front... Deque is now: [ in the ] >ps Size of deque is 2 >if cat Inserting "cat" at front... Deque is now: [ cat in the ] >ir hat Inserting "hat" at rear... Deque is now: [ cat in the hat ] >pf Front item is cat >pr Rear item is hat >rr Removing item at rear... Deque is now: [ cat in the ] >rf Removing item at front... Deque is now: [ in the ] >rf Removing item at front... Deque is now: [ the ] >ir junk Inserting "junk" at rear... Deque is now: [ the junk ] >rr Removing item at rear... Deque is now: [ the ] >rf Removing item at front... Deque is now: [ ] >if cow Inserting "cow" at front... Deque is now: [ cow ] >q Goodbye. |
Interestingly, the DequeViaLink1 class doesn't really need the instance variable front! In this activity, you are to develop a new class, DequeViaLink1NoFront, which from a client's point of view behaves exactly the same as DequeViaLink1.
Start by copying DequeViaLink1.java into DequeViaLink1NoFront.java, as the source code in the latter will be a relatively modest modification of that in the former.
The "trick" to making this work is for the linked structure formed by the Link1 nodes to be circular rather than grounded. Specifically, we make it so that the next field of the node corresponding to the rear of the deque points to the node corresonding to the front of the deque. (In the diagram above, that would mean that the ELK node's next field would point to the DOG node, rather than having value null.)
Essentially, if not quite literally, anywhere that front is used in the original class, you can replace it by rear.next.
To test your work, you can use the same tester program as you did in Activity #1. However, you need to make a small, but obvious, modification to the main() method.