CMPSCI 187: Programming With Data Structures

David Mix Barrington

Fall, 2011

Discussion Assignment #5: Double-Ended Queues

12 October 2011

This week in lecture we are introducing queues, both as presented in L&C with the QueueADT interface and as they exist in java.util with the Queue interface. A queue is a linear data structure that supports adding at the rear and removing from the front -- the QueueADT interface has methods enqueue, dequeue, and first (look at first element without removing it), along with isEmpty and size.

Here we define a related linear data structure called a deque for "double-ended queue" -- the word is pronounced like "deck" and should not be confused with the "dequeue" operation which is pronounced like the letters "DQ". In a deque we can add, remove, and look at elements at either end -- its methods are:


    public interface Deque {
       public void addToFront (T element);
       public T removeFront ( ) throws EmptyCollectionException;
       public T first ( ) throws EmptyCollectionException;
       public void addToRear (T element);
       public T removeRear ( ) throws EmptyCollectionException;
       public T last ( ) throws EmptyCollectionException;
       public boolean isEmpty ( );
       public int size ( );}

We'll only start to think about implementing deques this week if we have time -- this afternoon we will show how to implement queues with either circular arrays of linked data structures. Today we will be primarily just getting familiar with deques.

Exercises:

Question 1: One again assume that we have the Dog class defined and that we have declared and created Dog objects named ace, biscuit, cardie, and duncan. Trace a deque through the following sequence of operations, starting with an empty deque:


addToRear (cardie); addtoFront (duncan); addToFront (biscuit);
removeFront ( ); addToRear (ace); removeFront ( ); addToRear (cardie);
addToRear (duncan); removeFront ( ); removeRear ( ); addToFront
(biscuit);

Question 2: Write complete code for two new classes: DequeStack and DequeQueue, each of which will extend a class DequeClass that implements Deque. The class DequeStack should have the methods push, pop, and peek, and the class DequeQueue should have the methods enqueue. dequeue, and first. Implement these methods by calling on the public methods of DequeClass. You may leave out the constructors. (Why?)

Question 3: Write a method public void switchLastTwo to go into DequeClass, using Deque methods to duplicate the functionality of last week's method with the same name in the DogTeam class. Remember that last week's method never threw an exception, and did nothing when called by a collection with zero or one element.

Question 4: (only if time) Do you think it will be easy to implement a deque with a circular array like that you used in Project 3? Why or why not? Ignore for now what happens if the queue exceeds the array's capacity.

Question 5: (only if time) Do you think it will be easy to implement a deque with (singly-linked) linear nodes like those we used for ArrayStack and DogTeam? Why or why not?

Last modified 13 October 2011