www.pudn.com > GaitRsystem.rar > stack.h


// file stack.h 
// formula-based stack 
 
#ifndef Stack_ 
#define Stack_ 
 
#include "xcept.h" 
 
 
template 
class Stack { 
// LIFO objects 
   public: 
      Stack(int MaxStackSize = 10); 
      ~Stack() {delete [] stack;} 
      bool IsEmpty() const {return top == -1;} 
      bool IsFull() const {return top == MaxTop;} 
      T Top() const; 
      Stack& Add(const T& x); 
      Stack& Delete(T& x); 
   private: 
      int top;    // current top of stack 
      int MaxTop; // max value for top 
      T *stack;   // element array 
}; 
 
template 
Stack::Stack(int MaxStackSize) 
{// Stack constructor. 
   MaxTop = MaxStackSize - 1; 
   stack = new T[MaxStackSize]; 
   top = -1; 
} 
 
template 
T Stack::Top() const 
{// Return top element. 
   if (IsEmpty()) throw OutOfBounds(); // Top fails 
   return stack[top]; 
} 
 
template 
Stack& Stack::Add(const T& x) 
{// Add x to stack. 
   if (IsFull()) throw NoMem(); // add fails 
   stack[++top] = x; 
   return *this; 
} 
 
template 
Stack& Stack::Delete(T& x) 
{// Delete top element and put in x. 
   if (IsEmpty()) throw OutOfBounds(); // delete fails 
   x = stack[top--]; 
   return *this; 
} 
 
#endif