CSC 372
Laboratory:  Maintaining a Stack Class

File Needed: stack.h, stack.cpp, 372lab5.cpp

Background: "stack.h" contains a definition of a class named Stack. It is a simple 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.

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 Second( T & item ); //returns true if there is an element in the stack below the top,

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

bool Swap(); //returns true if there are two items in the stack; false otherwise. If true

//the top two items in the stack are exchanged.

Hand in your program with all three files above: stack.h and stack.cpp will be modified, 372lab5.cpp will not be modified.

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