CSC 372
Laboratory:  Stack Class



"stack.h" contains a definition of a class named Stack. It is a usual stack class that is templated. Stack() is the constructor. "void Push( T item )" is the function to place an item on the top of the stack. "T Pop()" returns the value of the top of the stack and removes it from the stack. "T Top()" returns the value of the top of the stack but does nothing with the stack. "bool Empty()" returns true if the stack is empty, false otherwise.

"stack.cpp" contains the implementations of these member functions. "lab3.cpp" is a program that instantiates three stacks containing integer, Boolean, and real values and loads and dumps their values. The order of compilation is, of course, stack.h->stack.cpp->lab3.cpp.

Your job is to modify both stack.h and stack.cpp to accommodate for the following new member functions

int Size(); //returns the number of elements in the stack.

//HINT: you may want to have a size variable in the private part to

//keep track of the number of times an elements are "poped" and "pushed"

bool Peek ( T & item, int N ); //returns true if there is an element in the Nth

//position on the stack, false otherwise. T is a copy of that element.

bool Set_Top( T & item); //returns true if there is a top element, false otherwise

//T is a copy of the top element in the stack

bool Set_Next( T & item ); //returns true if there is a next element in the stack,

//false otherwise. T is a copy of that element.

Make the appropriate changes to the files stack.h and stack.cpp. Hand in only these files on a 3.5" diskette. Your instructor will check them with my own version that will check if you have implemented these correctly.

Computer Science Department NSU.
Permission granted for non-commercial use only.