Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 7

Homework 7: The Stack ADT
List-Based 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 List object. The List implementation that you use may either be the one completed in Homework 6 or the one completed in Homework 5. This implementation allocates the List object when the Stack is created, and returns the storage when the Stack is deconstructed. The efficiency of the push() and pop() operations depend on the efficiency of the underlying List insert() and remove() operations. We would like for efficiency, and so it would be better to use the Linked implementation of the List, but stability is more important for the purposes of just this homework than efficiency.

Attributes

Operations

Note in the following description of the operations, I will use the Item type to represent the base type of the elements in the Stack. In your implementation, you may assume that Item is globally defined in the "lowest level" header file. It would be defined (globally) in ListArray.h if using the array implementation of a List, or in Node.h if using the linked implementation of a List.

Dependent Definitions

MAX_ITEMS: a named constant specifying the maximum number of items in a list.

Constructor/Destructor

Stack ( )

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

Stack ( int capacity )

Preconditions: 0 <= capacity <= MAX_SIZE
Postconditions: Creates an empty stack capable of holding capacity data items of type Item. 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 Item & 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.

Item 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.

const 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.

Item top ( ) const

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

Print/String

void display ( ostream & stream )

Preconditions: Item type supports stream insertion to an output stream.
Postconditions: Outputs the contents of the stack to the given stream. Form of the output must be strictly adhered to. The output uses '(' and '^)' to denote the extent of the stack. The stack is output with the TOS at the right hand end. The output uses space separation between elements of the stack as well as between the beginning '(' and the first element, and the ending '^)' and the last element. The stack display should terminate with a newline.

Examples:

Assignment

Be sure and use the C++ Programming Style Guide and follow the conventions described there. Up to 20% 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 List object implementation. You are also required to implement a non-member function that overloads the stream insertion operator for Stack objects.

For this short assignment, you will be given a test driver program that allows demonstration of all of your operations. It allows the notion of two Stack objects, the "current" stack and the "alternate" stack. Single Stack object operations pertain to the "current" stack. For the copy constructor and the assignment overload, the "current" stack is the one that gets updated (is logically the lhs), and the "alternate" stack is the one that is copied from (is logically the rhs). There is also a command to switch the roles of the two stack, making the current the alternate and the alternate the current. Each command also outputs the command and its argument, if any.

Test Stack Driver Commands
Command
Action
Cc
Create a new Stack object (using the new C++ operator), making it the current Stack; the command is immediately followed by an integer (c) which defines the capacity of the stack. For a stack with 0 specified capacity, create a "default" (MAX_SIZE) capacitly stack.
D
Destroy the current Stack object
+x
The '+' command performs an push operation on the current stack. The value to insert (x) immediately follows the '+' and is of type consistent with the type of the stack (an integer stack for your submission). In general, this should be read into an Item typed variable.
-
Perform a pop operation, displaying the item popped from the stack
@
Display to cout the data item at the TOS (or the character sequence "EMPTY" if the stack is empty).
*
Display the entire stack, as specified above.
E
Report whether the stack is empty, printing TRUE or FALSE.
F
Report whether the stack is full, printing TRUE or FALSE.
=
Demonstrate assignment overload, assigning the alternate stack to the current stack.
&
Demonstrate copy constructor, creating a new current stack from the alternate stack.
~
Switch the current stack with the alternate stack.
X
Delete all elements in the stack.
#
Ignore all following characters up to the following newline, outputing these characters as its argument.
H
Print a help message with the supported commands and a brief description of their actions.
Q
Quit the test program.

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

Helpful Files

File Description/Use
hw6Test.o

This is the object file for my test program. If your Stack class conforms to the public interface given above, then you should be able to test your Stack class with my client driver. To create an executable for the above scenario, you would issue the following command:

g++ -o hw6Test <List source .cpp files> Stack.cpp hw6Test.o

which compiles your Stack.cpp file and combines it with your List source files and my hw4Test.o object file into an executable named hw6Test.

hw6Test

This is a complete executable for a Stack of ints as required by this assignment. You can use this if you have any questions about how the driver should work.

When you download an executable like this from the Web, Linux needs to be "informed" that his is an executable file. This can be accomplished with the following command:

chmod u+x hw6Test

and you should do this after downloading and before trying to execute this test program.