home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX17011.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  5.7 KB  |  185 lines

  1. // \EXAMPLES\EX17011.H      - AStack.H
  2.  
  3. //----------------------------------------------------------
  4. // Example of template container classes implenenting stacks.
  5. // This version includes exception handling and templates.
  6. //----------------------------------------------------------
  7. //----------------------------------------------------------
  8. //  Exception handling is supported only by
  9. //                      the IBM C++ Set/2 compiler
  10. //  Templates are not supported by
  11. //                      the Microcoft Visual C++ compiler
  12. //  Two versions of this program are included:
  13. //                      EX1701I.EXE - for IBM CSet II
  14. //                      EX1701B.EXE - for Borland Turbo C++
  15. //----------------------------------------------------------
  16. // Files in this example:
  17. // %F,15,EX17010.H%EX17010.H       Stack.H       base class stack
  18. // EX17011.H       this file -- AStack.H      array stack
  19. // %F,15,EX17011.CPP%EX17011.CPP     AStack.CPP
  20. // %F,15,EX17012.H%EX17012.H       LLStack.H     derived linked list stack
  21. // %F,15,EX17012.CPP%EX17012.CPP     LLStack.CPP
  22. // %F,15,EX17013.H%EX17013.H       Exception.H
  23. // %F,15,EX17014.H%EX17014.H       Iterator.H       iterator class
  24. // %F,15,EX1701.CPP%EX1701.CPP      main() with exception handling
  25. // %F,15,EX1701B.CPP%EX1701B.CPP     main() without exception handling
  26. //----------------------------------------------------------
  27. #ifndef INCL_EX17011_H
  28. #define INCL_EX17011_H
  29.  
  30. #include "EX17010.H"
  31. #include "EX17014.H"
  32.  
  33. //---------------------------------------------------------
  34. // TEMPLATE: class AStack: is used to define a class stack
  35. //                         based on a array list which
  36. //                         stores elements of type T
  37. //
  38. // COMMENTS: Notice all functions are pure virtuals
  39. //---------------------------------------------------------
  40. template<class T> class AStack : public Stack<T>
  41. {
  42.    friend class AStackIter<T>;
  43.  
  44.    unsigned long free;  // number of free elements in the array
  45.    unsigned long size;  // size of the array
  46.    T        *pArray;    // pointer to the array
  47.  
  48.  
  49. public:
  50.    //------------------------------------------------------
  51.    // FUNCTION: Constructor allocates an array of specified
  52.    //                       size
  53.    // PARAMETERS: long capacity the max number of element
  54.    //                           required
  55.    // RETURN VALUE: none
  56.    // EXCEPTIONS THROWN: int if memory allocation error
  57.    //------------------------------------------------------
  58.    AStack( unsigned long capacity = 10);
  59.  
  60.    //------------------------------------------------------
  61.    // FUNCTION: Destructor deallocates array
  62.    // PARAMETERS: none
  63.    // RETURN VALUE: none
  64.    //------------------------------------------------------
  65.    ~AStack();
  66.  
  67.    //------------------------------------------------------
  68.    // FUNCTION: Push pushes an element of type T onto the
  69.    //                stack
  70.    // PARAMETERS: T y is an element of type T
  71.    // RETURN VALUE: none
  72.    // EXCEPTIONS: throws an int if full
  73.    //------------------------------------------------------
  74.    void
  75.    Push( const T y);
  76.  
  77.    //------------------------------------------------------
  78.    // FUNCTION: Pop pops an element of type T from the stack
  79.    // PARAMETERS: none
  80.    // RETURN VALUE: returns the top element of the stack
  81.    // EXCEPTIONS: throws an int if empty
  82.    //------------------------------------------------------
  83.    T
  84.    Pop();
  85.  
  86.    //------------------------------------------------------
  87.    // FUNCTION: Full returns true if stack is full, false
  88.    //                otherwise
  89.    // PARAMETERS: none
  90.    // RETURN VALUE: int
  91.    //------------------------------------------------------
  92.    inline int
  93.    Full() const;
  94.  
  95.    //------------------------------------------------------
  96.    // FUNCTION: Empty returns true if stack is empty, false
  97.    //                 otherwise
  98.    // PARAMETERS: none
  99.    //
  100.    // RETURN VALUE: int
  101.    //------------------------------------------------------
  102.    inline int
  103.    Empty() const;
  104.  
  105.    //------------------------------------------------------
  106.    // FUNCTION: OnTop returns the top element of the stack
  107.    // PARAMETERS: none
  108.    // RETURN VALUE: T the top element of the stack
  109.    // EXCEPTIONS: throws an int if empty
  110.    //------------------------------------------------------
  111.    T
  112.    OnTop() const;
  113.  
  114.    //------------------------------------------------------
  115.    // FUNCTION: NumElem returns number of elements in stack
  116.    // PARAMETERS: none
  117.    // RETURN VALUE: long the number of elements in the stack
  118.    //------------------------------------------------------
  119.    virtual long
  120.    NumElem() const;
  121. };
  122.  
  123.  
  124. template<class T> inline int
  125. AStack<T>::Full() const
  126. {
  127.    // if free is zero return true, false otherwise
  128.    return ( free == 0);
  129. }
  130.  
  131. template<class T> inline int
  132. AStack<T>::Empty() const
  133. {
  134.    // if free equals size return true, false otherwise
  135.    return ( free == size);
  136. }
  137.  
  138.  
  139. template<class T> inline long
  140. AStack<T>::NumElem() const
  141. {
  142.    return (size - free);
  143. }
  144.  
  145.  
  146. //
  147. // Use to iterate through the elements of a stack
  148. //
  149. template <class T> class AStackIter : public Iterator<T>
  150. {
  151.    long             index;
  152.    const T*         pArray;
  153.    const AStack<T>* pStack;
  154.  
  155. public:
  156.    // FUNCTION: constructor
  157.    // PARAMETERS:
  158.    AStackIter( const AStack<T>& stack);
  159.  
  160.    // FUNCTION: operator()
  161.    // RETURN VALUE: the next element
  162.    T&
  163.    operator()();
  164. };
  165.  
  166.  
  167. template<class T>
  168. AStackIter<T>::AStackIter( const AStack<T>& stack)
  169.  : index(0), pArray( stack.pArray), pStack(&stack)
  170. {
  171. }
  172.  
  173.  
  174. template<class T> T&
  175. AStackIter<T>::operator()()
  176. {
  177.    if (pStack->Empty()) {
  178.       throw ( AStack<T>::EmptyExcept());
  179.    }
  180.    return (T&)pArray[index++];
  181. }
  182.  
  183.  
  184. #endif
  185.