Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 10

Homework 10: The Queue ADT
Circular Array and Linked Implementations

Overview

In this homework, you will implement the Queue ADT using two implementations -- a circular array based implementation and a linked implementation. The linked implementation provides efficient implementation of enqueue and dequeue operations by allowing both ends of the list to move through the indices of the array and "wrap around" the end of the array back to the beginning of the array. This gives the circular part of the implementation. Like earlier implementations, the linked implementation allocates memory for each Node as it is inserted into the structure through an enqueue, and deallocates the Node structure (or class) when it is removed from the stack during a dequeue operation. The linked implementation exhibits the same efficiency for enqueue and dequeue operations, but avoids the wasted space for the unused portion of a queue that is entailed in the array implementation.

This assignment will use template Queue classes so that we can define queues of integers or queues of doubles or queues of characters easily. We will keep the application simple, limited to a general purpose driver program.

Attributes/Structure

Operations

Note in the following description of the operations, I will use DT to abstractly represent the data type of the elements of the queue. In the template impementation of the Queue class, this will be the passed template type.

Constructor/Destructor

Queue ( )

Preconditions: None.
Postconditions: Creates an empty queue capable of holding MAX_SIZE data items of type DT. Beginning and end of the queue indicate no element.

Queue ( int capacity )

Preconditions: None.
Postconditions: Creates an empty stack capable of holding capacity data items of type DT. The capacity parameter can be ignored in the linked implementation. Beginning and end of the queue indicate no element.

Queue ( const Queue & orig )

Preconditions: None.
Postconditions: Creates a new queue that is a copy of the given orig queue. Note that the new queue shares no storage with the original queue.

~Queue ( )

Preconditions: None.
Postconditions: Destroys the queue, cleaning up all resources associated with the object.

Mutator(s)

void enqueue ( const DT & item )

Preconditions: Queue is not full (i.e. the number of items currently in the queue is less than its capacity).
Postconditions: The item has been inserted at the end of the sequence, and the end is positioned at the newly inserted item.

DT dequeue ( )

Preconditions: The queue is not empty.
Postconditions: The item at the beginning of the queue is removed and returned. The beginning becomes the item's successor in the sequence, or represents EMPTY if the queue is now empty.

void clear ( )

Preconditions: None.
Postconditions: The queue is empty and all resources have been returned to the system.

Queue & operator= ( const Queue & rhs )

Preconditions: None.
Postconditions: Previous queue resources have been returned to the system and the queue has become a copy of the given Queue rhs. The current queue is returned.

Observers (Predicates and Accessor(s))

bool isEmpty ( ) const

Preconditions: None.
Postconditions: Return value is true if the queue contains no items, and false otherwise.

bool isFull ( ) const

Preconditions: None.
Postconditions: Return value is true if the queue contains its capacity of items, and false otherwise.

DT peek ( ) const

Preconditions: The queue is not empty.
Postconditions: Return value is a copy of the item at the beginning of the queue. The queue is unaffected.

Assignment

Be sure and use the C++ Programming Style Guide and follow the conventions described there. Up to 30% of the grade for this homework will be based on following these conventions and practicing good documentation.

Your task is to implement the Stack ADT using a circular array implementation and a doubly linked implementation. You must use a template class definition so that we can easily define different types of queues in the same program (although a given application program will either use one implementation or the other-- not both). You are also required to implement a non-member function that overloads the stream insertion operator for Queue objects. Please be sure that the stream insertion indicates which end of the sequence is the beginning and the end of the queue.

You must create a test plan to thoroughly test all of your Queue operations, and demonstrate through use of a driver program that your operations perform correctly across the different tests of your test plan. Submit the execution of your test plan by cutting and pasting from your Terminal window into a text editor and saving the file with the name testoutput.txt. You can use the same driver program for both the circular array and the linked implementations. It should be general and allow interactive testing of all operations, so there should be one key operations interpreted by the driver program (+ for enqueue, - for dequeue, @ to show current beginning of queue, C for clear, D for display the entire queue, A to demonstrate the assignment overload in a meaningful way, etc.).

Please instantiate your template Queue as both a Queue of integers and a Queue of chars, so you will have four runs of your test plan, Queue<int> and Queue<char> for both circular array and linked implementations.