
/**************************************************************************************************/
/*                                                                                                */
/*  Name:  DarC KonQuesT                                                                          */
/*  Program:  #4                                                                                  */
/*  Date:  2005.3.15                                                                              */
/*  Purpose:                                                                                      */
/*     Clone implementation of the STL stack class.  Allows for creation, manipulation, and use   */
/*   of stacks.                                                                                   */
/*                                                                                                */
/*                                                                                                */
/**************************************************************************************************/
#ifndef H_stack
#define H_stack

using namespace std;

template <class Type>
class stack
{
  
  public:
    stack();
    ~stack();
    bool empty();
    void pop();
    void push(Type data);
    int size();
    Type top();
  private:
    struct nodeType  //struct declaration - doubly linked
    {
      Type info;
      nodeType *next;
      nodeType *back;
    };
    
   nodeType *top_node; 
   int size_of;
    
};

/*  Name:  stack() ~ constructor                                                                                      */
/*  Purpose: initialize variables                                                                                     */
/*  Parameters: none                                                                                                  */
/*  Preconditions: variables are declared, stack class is declared                                                    */
/*  Postconditions:                                                                                                   */

template <class Type>
stack<Type>::stack()
{
  size_of = 0;
  top_node = NULL;
};



/*  Name:  ~stack()  ~ destructor                                                                                     */
/*  Purpose:  destroy stack                                                                                           */
/*  Parameters:  None                                                                                                 */
/*  Preconditions:  stack may exist                                                                                   */
/*  Postconditions:  stack is emptied                                                                                 */

template <class Type>
stack<Type>::~stack()
{
  while(!empty())
  {
    nodeType *temp = top_node;
    top_node = top_node->back;
    delete temp;
  }
};



/*  Name:  empty()                                                                                                    */
/*  Purpose:  tell user whether or not the stack is empty                                                             */
/*  Parameters: none                                                                                                  */
/*  Preconditions: size_of is declared and kept up to date                                                            */
/*  Postconditions:  returns TRUE if stack is empty; FALSE if not empty                                               */

template <class Type>
bool stack<Type>::empty()
{
  return(size_of == 0);
};


/*  Name:  pop()                                                                                                      */
/*  Purpose:  Remove the top item from the stack                                                                      */
/*  Parameters:  None                                                                                                 */
/*  Preconditions:  Stack exists, size_of is up to date.                                                              */
/*  Postconditions:                                                                                                   */

template <class Type>
void stack<Type>::pop()
{
  nodeType *temp = top_node;
  if(top_node != NULL)
  {
    top_node = top_node->back;
    delete temp;
    size_of--;
  }
};


/*  Name:  push(Type)                                                                                                 */
/*  Purpose:  Place a new item on the stack                                                                           */
/*  Parameters:  None                                                                                                 */
/*  Preconditions: data is declared/filled and passed correctly                                                       */
/*  Postconditions:  new item is added to stack, size_of is updated                                                   */

template <class Type>
void stack<Type>::push(Type data)
{
  nodeType *temp = new nodeType;
  temp->info = data;
  temp->back = top_node;
  temp->next = NULL; 
  top_node = temp;
  size_of++;
};


/*  Name:  size()                                                                                                     */
/*  Purpose: Return size of the stack                                                                                 */
/*  Parameters: None                                                                                                  */
/*  Preconditions:  size_of must be declared, initialized and kept up to date                                         */
/*  Postconditions: Number of items in the stack is returned to caller                                                */

template <class Type>
int stack<Type>::size()
{
  return(size_of);
};


/*  Name:  top()                                                                                                      */
/*  Purpose:  return the top item on the stack                                                                        */
/*  Parameters:  None                                                                                                 */
/*  Preconditions: stack is initialized and top_node is correct                                                       */
/*  Postconditions:  top item is returned to caller                                                                   */

template <class Type>
Type stack<Type>::top()
{
  return (top_node->info);
};
#endif

