//Programmer: Bo Bayles  Date: 30 November 2004
//File: Stack.h          Class: CS54 C
//Purpose: This is the header file for the Stack class.

#ifndef STACK_H
#define STACK_H

#include <iostream>

const int iMAXSIZE = 16;
//iMAXSIZE represents the largest possible stack.

template<class T>
class Stack
{
  private:
    T pos[iMAXSIZE];
    //The values of the stack are stored in the array.
    int iTop;
    //The value of the top of the stack is stored in iTop.

  public:
    Stack();
    //Default constructor: Initialize the first stack element to -1.

    void push(T x);
    //Push function: Accepts a variable of the type specified by the
    //  programmer via the template. Copies the value of x to the
    //  pos array and increments the variable that represents the stack's top.

    T pop();
    //Pop function: Accepts nothing. Decrements the value that represents the
    //  stack's top, then if the top is a valid position in the pos array,
    //  returns the value stored in that position of the array. Otherwise
    //  an error is outputted and the top position is returned to its original
    //  value.
};
#endif

template<class T>
Stack<T>::Stack()
{
  iTop = 0;
  pos[0] = -1;
}
//Set the top position to 0, and initialize pos[0] to -1.

template<class T>
void Stack<T>::push(T x)
{
  if (iTop < iMAXSIZE)
  {
    pos[iTop] = x;
    iTop++;
  }
  //Only add to the stack at position iTop if it's not full.
  else
    cout << "Error: The stack is full." << endl;

  return;
}

template<class T>
T Stack<T>::pop()
{
  T retValue = 0;
  //Declare a variable to represent the return value.

  iTop--;
  //Decrement the top position to get the array into the position of the value
  //  that was last pushed (since we increment right after pushing).
  if (iTop >= 0)
    retValue = pos[iTop];
  //If iTop is 0 or greater, a valid array position, set the return valye to
  //  the position of the array indicated by iTop.
  else
  {
    cout << "Error: The stack is empty." << endl;
    iTop++;
    //Make the array position positive (or 0) again.
  }

  return retValue;
}

