Lab 6: Linked List Implementation of a Queue

Objectives

The objectives of this lab are:

Introduction

Download the following Java files into your lab6 directory:

You are provided a complete implementation (from Goodrich and Tamassia) of a doubly linked list (DList) of (DLNode). Before getting started, study the example of the DListStack class, an implementation of the Stack ADT based on the DList.

The relevant methods of the DList and DLNode classes are shown in the following UML diagrams:

DList


+ DList()
+ size(): int
+ isEmpty(): boolean
+ getFirst(): DLNode throws IllegalStateException
+ getLast(): DLNode throws IllegalStateException
+ getPrev(DLNode v): DLNode throws IllegalArgumentException
+ getNext(DLNode v): DLNode throws IllegalArgumentException
+ addBefore(DLNode v, DLNode z) throws IllegalArgumentException
+ addAfter(DLNode v, DLNode z)
+ addFirst(DLNode v)
+ addLast(DLNode v)
+ remove(DLNode v)
+ hasPrev(DLNode v): boolean
+ hasNext(DLNode v): boolean
+ toString(): String
DNode


+ DLNode()
+ DLNode(Object, DLNode, DLNode)
+ setElement(Object)
+ setNext(DLNode)
+ setPrev(DLNode)
+ getElement(): Object
+ getNext(): DLNode
+ getPrev(): DLNode

The DListStack class uses a DList to provide an implementation of a Stack:

DListStack
- dl: DList
+ DListStack()
+ DLNode(Object, DLNode, DLNode)
+ size(): int
+ isEmpty(): boolean
+ top(): Object throws EmptyStackException
+ push(Object)
+ pop(): Object throws EmptyStackException

Implementing a Stack from a DList is simple. The Stack's push() method is the DList's addFirst() method. The Stack's top() method is the DList's getFirst() method. Thus the stack can be implemented by using a DList to store the stack's elements and calling the appropriate DList methods for push(), pop() and so forth.

Here's an example:

    private DList dl;
    
    public DListStack() {
        dl = new DList();  // A DList stores the stack's elements
    }
    public void push (Object element) {
        dl.addFirst(new DLNode(element, null,null));
    }

See the downloaded implementation for further details. Pay special attention to the pop() method. And note how the Stack is tested in the main() method.

Part I Problem

Develop a complete implementation of a DListQueue class that implements the Queue interface. You can download Queue interface and exception classes here:

Make sure you design a main() method that thoroughly tests your implementation. A good problem to test a Queue on would be to store the characters of the string in the queue and then remove them all. They should come out in the same order they went in.

Part II

A Deque (pronounced deck) is a double-ended queue. It allows insertions and deletions from both ends of a list. Develop a complete implementation of a DListDeque class that implements the Deque interface. A deque is defined abstractly as follows:

You can download Deque interface and exception classes here:

The Railroad Yard Problem. Make sure you design a main() method that thoroughly tests your implementation and demonstrates its correctness. Here's a test case: Insert the numbers 1 through 8 into the deque in that order. Then use deque operations to print the numbers in the following sequence: 1, 2, 3, 5, 4, 6, 7, 8, ending with an empty deque. This might be called the railroad yard problem. Imagine train cars coming into a railroad yard in numerical order, 1..8. Your job is to send them down the track in some random order. The railroad yard has a physical version of a deque that you can use to rearrange cars.

Step-wise Refinement

Remember to use stepwise refinement. Code and test small portions of your classes before moving on.

Documentation

Provide proper Javadoc documentation for all your source code.

You're done. Great work!