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

  1. // \EXAMPLES\EX17012B.H      - LLStack.H
  2.  
  3. //----------------------------------------------------------
  4. // Example of template container classes implenenting stacks.
  5. // This version includes templates but no exception handling.
  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,EX17010B.H%EX17010B.H       Stack.H       base class stack
  18. // %F,15,EX17011B.H%EX17011B.H       AStack.H      derived array stack
  19. // %F,15,EX17011B.CPP%EX17011B.CPP     AStack.CPP
  20. // EX17012B.H       this file -- LLStack.H     linked list stack
  21. // %F,15,EX17012B.CPP%EX17012B.CPP     LLStack.CPP
  22. // %F,15,EX17013B.H%EX17013B.H       Exception.H
  23. // %F,15,EX17014B.H%EX17014B.H       Iterator.H       iterator class
  24. // %F,15,EX1701B.CPP%EX1701B.CPP      main() without exception handling
  25. // %F,15,EX1701.CPP%EX1701.CPP       main() with exception handling
  26. //----------------------------------------------------------
  27. #ifndef INCL_EX17012_H
  28. #define INCL_EX17012_H
  29.  
  30. #include "EX17010B.H"
  31. #include "EX17014B.H"
  32.  
  33. //---------------------------------------------------------
  34. // TEMPLATE: class LLStack: is used to define a class stack
  35. //                          based on a linked list which
  36. //                          stores elements of type T
  37. //
  38. // COMMENTS: Notice all functions are pure virtuals
  39. //---------------------------------------------------------
  40. template<class T> class LLStack : public Stack<T>
  41. {
  42.  
  43.    struct Node
  44.    {
  45.       public:
  46.       T     datum;
  47.       Node* next;
  48.    };
  49.  
  50.    friend class LLStackItr<T>;
  51.  
  52.  
  53.    Node*    pList;
  54.    unsigned long used;
  55.    long     lRc;
  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: aborts 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: aborts 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: aborts 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 LLStackItr : 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.    LLStackItr( 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. LLStackItr<T>::LLStackItr( const LLStack<T>& stack)
  174.  : pNode( stack.pList), pStack(&stack)
  175. {
  176. }
  177.  
  178.  
  179. template<class T> T&
  180. LLStackItr<T>::operator()()
  181. {
  182.    LLStack<T>::Node* pTempNode = pNode;
  183.  
  184.    if (pStack->Empty()) {
  185.       cout << "Stack Empty: aborting." << endl;
  186.       abort();
  187.    }
  188.    else if (pNode == (LLStack<T>::Node*)NULL)
  189.    {
  190.       cout << "Null Pointer: Aborting." << endl;
  191.       abort();
  192.    }
  193.    pNode = pNode->next;
  194.    return (T&)pTempNode->datum;
  195. }
  196.  
  197.  
  198. #endif
  199.