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

  1. // \EXAMPLES\EX17012.H      - LLStack.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. // %F,15,EX17011.H%EX17011.H       AStack.H      derived array stack
  19. // %F,15,EX17011.CPP%EX17011.CPP     AStack.CPP
  20. // EX17012.H       this file -- LLStack.H     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. // \EXAMPLES\EX17012.H      - LLStack.H
  28.  
  29. #ifndef INCL_EX17012_H
  30. #define INCL_EX17012_H
  31.  
  32. #include "EX17010.H"
  33. #include "EX17014.H"
  34.  
  35. //---------------------------------------------------------
  36. // TEMPLATE: class LLStack: is used to define a class stack
  37. //                          based on a linked list which
  38. //                          stores elements of type T
  39. //
  40. // COMMENTS: Notice all functions are pure virtuals
  41. //---------------------------------------------------------
  42. template<class T> class LLStack : public Stack<T>
  43. {
  44.    friend class LLStackIter<T>;
  45.  
  46.    struct Node
  47.    {
  48.       T     datum;
  49.       Node* next;
  50.    };
  51.  
  52.    Node*    pList;
  53.  
  54.    unsigned long used;
  55.  
  56. public:
  57.    //------------------------------------------------------
  58.    // FUNCTION: Constructor initiallizes member data
  59.    // PARAMETERS: none
  60.    // RETURN VALUE: none
  61.    //------------------------------------------------------
  62.    LLStack();
  63.  
  64.    //------------------------------------------------------
  65.    // FUNCTION: Destructor deallocates nodes
  66.    // PARAMETERS: none
  67.    // RETURN VALUE: none
  68.    //------------------------------------------------------
  69.    ~LLStack();
  70.  
  71.    //------------------------------------------------------
  72.    // FUNCTION: Push pushes an element of type T onto the
  73.    //                stack
  74.    // PARAMETERS: T y is an element of type T
  75.    // RETURN VALUE: none
  76.    // EXCEPTIONS: throws an int if full
  77.    //------------------------------------------------------
  78.    void
  79.    Push( const T y);
  80.  
  81.    //------------------------------------------------------
  82.    // FUNCTION: Pop pops an element of type T from the stack
  83.    // PARAMETERS: none
  84.    // RETURN VALUE: returns the top element of the stack
  85.    // EXCEPTIONS: throws an int if empty
  86.    //------------------------------------------------------
  87.    T
  88.    Pop();
  89.  
  90.    //------------------------------------------------------
  91.    // FUNCTION: Full returns true if stack is full, false
  92.    //                otherwise
  93.    // PARAMETERS: none
  94.    // RETURN VALUE: int
  95.    //------------------------------------------------------
  96.    inline int
  97.    Full() const;
  98.  
  99.    //------------------------------------------------------
  100.    // FUNCTION: Empty returns true if stack is empty, false
  101.    //                 otherwise
  102.    // PARAMETERS: none
  103.    //
  104.    // RETURN VALUE: int
  105.    //------------------------------------------------------
  106.    inline int
  107.    Empty() const;
  108.  
  109.    //------------------------------------------------------
  110.    // FUNCTION: OnTop returns the top element of the stack
  111.    // PARAMETERS: none
  112.    // RETURN VALUE: T the top element of the stack
  113.    // EXCEPTIONS: throws an int if empty
  114.    //------------------------------------------------------
  115.    T
  116.    OnTop() const;
  117.  
  118.    //------------------------------------------------------
  119.    // FUNCTION: NumElem returns number of elements in stack
  120.    // PARAMETERS: none
  121.    // RETURN VALUE: long the number of elements in the stack
  122.    //------------------------------------------------------
  123.    virtual long
  124.    NumElem() const;
  125. };
  126.  
  127.  
  128. template<class T> inline long
  129. LLStack<T>::NumElem() const
  130. {
  131.    return (used);
  132. }
  133.  
  134.  
  135. template<class T> inline int
  136. LLStack<T>::Full() const
  137. {
  138.    // return false, never full
  139.    return (0);
  140. }
  141.  
  142.  
  143. template<class T> inline int
  144. LLStack<T>::Empty() const
  145. {
  146.    // if pList is NULL Stack is empty
  147.    return ( pList == (Node *)NULL);
  148. }
  149.  
  150.  
  151. //
  152. // Use to iterate through the elements of a stack
  153. //
  154. template <class T> class LLStackIter : public Iterator<T>
  155. {
  156.    LLStack<T>::Node*    pNode;
  157.    const T*             pArray;
  158.    const LLStack<T>*    pStack;
  159.  
  160. public:
  161.    // FUNCTION: constructor
  162.    // PARAMETERS:
  163.    LLStackIter( const LLStack<T>& stack);
  164.  
  165.    // FUNCTION: operator()
  166.    // RETURN VALUE: the next element
  167.    T&
  168.    operator()();
  169. };
  170.  
  171.  
  172. template<class T>
  173. LLStackIter<T>::LLStackIter( const LLStack<T>& stack)
  174.  : pNode( stack.pList), pStack(&stack)
  175. {
  176. }
  177.  
  178.  
  179. template<class T> T&
  180. LLStackIter<T>::operator()()
  181. {
  182.    LLStack<T>::Node* pTempNode = pNode;
  183.  
  184.    if (pStack->Empty()) {
  185.       throw( LLStack<T>::EmptyExcept());
  186.    }
  187.    else if (pNode == (LLStack<T>::Node*)NULL)
  188.    {
  189.       throw( LLStack<T>::EmptyExcept());
  190.    }
  191.    pNode = pNode->next;
  192.    return (T&)pTempNode->datum;
  193. }
  194.  
  195.  
  196. #endif
  197.