Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 8

Homework 8: The Stack ADT
Array Implementation

Overview

Next to the List ADT, the Stack is an abstract data type that recurs often throughout all the areas of computer science. It also comes up often in related fields such as Operations Research. We can think of the Stack as a List that is restricted in its operation, so that inserts and removes always happen from the same end of the List. This designated end of the stack is known as the top of stack (TOS). This restriction gives a consistent service discipline in which the last item added to the structure is the first item to be retrieved from the structure. This is known as a Last In First Out (LIFO) service discipline.

In this homework, you will implement the Stack ADT using a dynamically allocated array. This implementation allocates memory when the Stack is created, and returns the storage when the Stack is deconstructed. As long as the right end of the array is used to represent the top of stack, the insert operation (known as a push) and remove operation (known as a pop) are efficient.

Attributes

Structure

The key structure associated with the Stack ADT is the sequential notion of the collection referred to above -- with each data item except the first having a unique predecessor and each data item except the last having a unique successor.

Operations

Note in the following description of the operations, I will use DT to abstractly represent the data type of the elements of the stack. In the template impementation of the Stack class, this will be the passed template type. In the explicit definition of the Stack class, this would be the Item type.

Constructor/Destructor

Stack ( )

Preconditions: None.
Postconditions: Creates an empty stack capable of holding MAX_SIZE data items of type DT. Current TOS position is at EMPTY.

Stack ( int capacity )

Preconditions: None.
Postconditions: Creates an empty stack capable of holding capacity data items of type DT. Current TOS position is at EMPTY.

Stack ( const Stack & orig )

Preconditions: None.
Postconditions: Creates a new stack that is a copy of the given orig stack. Current TOS position is at the same position as in the orig.

~Stack ( )

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

Mutator(s)

void push ( const DT & item )

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

DT pop ( )

Preconditions: The stack is not empty.
Postconditions: The item at the current TOS position is removed from the stack. The TOS is positioned at the item's predecessor in the sequence, or represents EMPTY if the stack is now empty.

void clear ( )

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

Stack & operator= ( const Stack & rhs )

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

Observers (Predicates and Accessor(s))

bool isEmpty ( ) const

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

bool isFull ( ) const

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

DT top ( ) const

Preconditions: The stack is not empty.
Postconditions: Return value is a copy of the item at the TOS. The stack 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 dynamically allocated array implementation. Use either the Item class or a template version of the Stack class to increase useability. (Or, you can use a template class with the Item as the instantiated type.) Extra credit is awarded for a template version of the class. You also are required to implement a non-member function that overloads the stream insertion operator for Stack objects.

For an early submission, you need to include at least a test driver program that allows demonstration of all of your operations.

For the final submission, write an application that performs evaluation of integer postfix expressions by pushing operands onto the stack and, each time an operator is encountered in the input stream, pops the top two operands, performs the operation, and pushes the result back onto the stack. Upon completion, there should be one result remaining on the stack. This should be popped and the result printed out.

You will also detail a test plan that gives good coverage demonstrating that your implementation works and _then_ demonstrate execution of the test plan.