home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX15021.H
- //--------------------------------------------------------------
- // definition of the class templates ASTACK and ASTACKITR
- //--------------------------------------------------------------
-
- // files in this example:
- // EX15021.H this file
- // %F,15,EX15022.H%EX15022.H definition of String class
- // %F,15,EX15021.CPP%EX15021.CPP member functions of Astack
- // %F,15,EX1502.CPP%EX1502.CPP main() - exercise templates
-
- //--------------------------------------------------------------
- #ifndef ASTACK_H
- #define ASTACK_H
-
- //--------------------------------------------------------------
- #include <iostream.h>
-
- //---------------------------------------------------------
- // TEMPLATE: class AStack: is used to define a class stack
- // based on a array of type T
- //---------------------------------------------------------
- template<class T> class AStack
- {
- friend class AStackItr<T>; // iterator class
- T *pArray; // pointer to the array
- unsigned long size; // size of the array
- unsigned long free; // number of free elements
- public:
- //-----------------------------------------------------------
- // FUNCTION: Constructor allocates an array of specified size
- // PARAMETERS: long - capacity - the max number of elements
- // RETURN VALUE: none
- //-----------------------------------------------------------
- AStack( unsigned long capacity = 10);
- //-----------------------------------------------------------
- // FUNCTION: Destructor deallocates array
- // PARAMETERS: none
- // RETURN VALUE: none
- //-----------------------------------------------------------
- ~AStack();
- //-----------------------------------------------------------
- // FUNCTION: Push pushes an element of type T onto the stack
- // PARAMETERS: T y is an element of type T
- // RETURN VALUE: none
- //-----------------------------------------------------------
- void Push( const T y);
- //-----------------------------------------------------------
- // FUNCTION: Pop pops an element of type T from the stack
- // PARAMETERS: none
- // RETURN VALUE: returns the top element of the stack
- //-----------------------------------------------------------
- T Pop();
- //-----------------------------------------------------------
- // FUNCTION: Full returns true if stack is full, false
- // otherwise
- // PARAMETERS: none
- // RETURN VALUE: int
- //----------------------------------------------------------
- inline int Full() const;
- //----------------------------------------------------------
- // FUNCTION: Empty returns true if stack is empty, false
- // otherwise
- // PARAMETERS: none
- // RETURN VALUE: int
- //----------------------------------------------------------
- inline int Empty() const;
- //----------------------------------------------------------
- // FUNCTION: NumElem returns the number of elements in stack
- // PARAMETERS: none
- // RETURN VALUE: long the number of elements in the stack
- //------------------------------------------------------
- long NumElem() const;
- };
-
- //----------------------------------------------------------
- // Inline member functions of ASTACK
- //----------------------------------------------------------
-
- template<class T> inline int
- AStack<T>::Full() const
- { // if free is zero return true, false otherwise
- return ( free == 0);
- }
-
- template<class T> inline int
- AStack<T>::Empty() const
- { // if free equals size return true, false otherwise
- return ( free == size);
- }
-
-
- template<class T> inline long
- AStack<T>::NumElem() const
- { return (size - free);
- }
-
- //---------------------------------------------------------
- // TEMPLATE: class ASTACKITR
- // to iterate through the elements of a stack
- //---------------------------------------------------------
- template <class T> class AStackItr
- { long index;
- const T* pArray;
- const AStack<T>* pStack;
- public:
- // FUNCTION: constructor
- // PARAMETERS:
- AStackItr( const AStack<T>& stack);
-
- // FUNCTION: operator()
- // RETURN VALUE: the next element
- T& operator()();
- };
-
- //----------------------------------------------------------
- // Inline member functions of ASTACKITR
- //----------------------------------------------------------
- template<class T>
- AStackItr<T>::AStackItr( const AStack<T>& stack)
- : index(0), pArray( stack.pArray), pStack(&stack)
- {
- }
-
- template<class T> T&
- AStackItr<T>::operator()()
- { if (pStack->Empty())
- { cout << "Stack Empty: " << endl;
- abort();
- }
- return (T&)pArray[index++];
- }
-
- #endif
-