class stack
{
public:
stack(); //constructor
~stack() {delete [] SArray;}
//destructor deletes the array
//class methods
void Push(int S);
void Pop();
int Top();
// IsEmpty() tests to see if the TopOfStack equals -1 and returns true
or false.
int IsEmpty() {return TopOfStack == -1;}
// IsFull() tests to see if the TopOfStack is at position Maxsize-1
// and returns either true or false
int IsFull(){return TopOfStack == Maxsize-1;}
// MakeEmpty() sets the TopOfStack to equal -1.
void MakeEmpty() {TopOfStack = -1;}
private:
int Maxsize; // the size of the stack
int TopOfStack; // index for the top of the stack
int *SArray; // SArray is a pointer to an integer
};
// constructor initialises the private
members of stack
// array SArray is allocated dynamically
using new and is of size Maxsize
stack::stack()
{
Maxsize = 10;
TopOfStack = -1;
SArray = new int[Maxsize];
}
// Push places the integer S on the top of the stack
void stack::Push(int S)
{
if(!IsFull())
SArray[++TopOfStack] = S; //increment TopOfStack and place S in that array
position
else
cout<<"No room on stack\n";
}
// Pop decrements the TopOfStack index, thus indicating a new TopOfStack
void stack::Pop()
{
if(IsEmpty())
cout<<"Stack empty, nothing to pop\n";
else
TopOfStack--;
}
// Top returns the top element on the stack
int stack::Top()
{
return SArray[TopOfStack];
}