Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 9

Homework 9: The Stack ADT
Linked Implementation

Overview

In this homework, you will implement the Stack ADT using a linked implementation. This implementation allocates memory for each Node as it is inserted into the structure through a push, and deallocates the Node structure (or class) when it is removed from the stack during a pop operation. The linked implementation exhibits the same efficiency for push and pop operations, but avoids the wasted space for the unused portion of a stack that is entailed in the array implementation.

We will also use this reimplementation of the Stack ADT to exercise the use of stacks in our application program, where we will take an infix expression involving integers, operators, and parenthesis, and first convert it to postfix using a stack-based algorithm, and then use another stack to evaluate the postfix expression to generate a final result.

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.

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. Note that the new stack shares no storage with the original stack.

~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 singly linked implementation. You must use a template class definition so that we can easily define different types of stacks in the same program. You also are required to implement a non-member function that overloads the stream insertion operator for Stack objects. Please be sure that the stream insertion indicates which end of the sequence is the top of stack.

You must create a test plan to thoroughly test all of your Stack 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 will also create an application that uses Stacks. Your application program will input a line from the user that constitutes an infix expression, consisting of integer operands, the operators +, -, *, /, and beginning and ending parenthesis, and these tokens will always have white space between them. Given this infix expression, the program will first convert the infix expression into an equivalent postfix expression. The algorithm to perform this conversion uses a Stack whose elements are character types. Once the postfix expression is obtained, the program will evaluate the postfix expression using an integer stack in a manner similar to the last homework. Thus this program requires that we be able to create and use both a character stack and an integer stack in the same program.