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

  1. // \EXAMPLES\EX17012.CPP    - LLStack.CPP
  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. // %F,15,EX17012.H%EX17012.H       LLStack.H     derived linked list stack
  21. // EX17012.CPP     this file -- 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_EX17012_CPP
  28. #define INCL_EX17012_CPP
  29.  
  30. #include <stdlib.h>
  31. #include "EX17010.H"
  32. #include "EX17012.H"
  33.  
  34. template<class T>
  35. LLStack<T>::LLStack()
  36.    : pList( (Node *)NULL), used( 0)
  37. {
  38. }
  39.  
  40.  
  41. template<class T>
  42. LLStack<T>::~LLStack()
  43. {
  44.    Node* pTempNode = (Node *)NULL;  // points to node in stack
  45.  
  46.    //stop when pList is null
  47.    while( (pTempNode = pList) != (Node *)NULL)
  48.    {
  49.       pList = pList->next;        // remove node from stack
  50.       delete pTempNode;           // deallocate node
  51.    }
  52.    pTempNode = (Node *)NULL;
  53. }
  54.  
  55.  
  56. template<class T> void
  57. LLStack<T>::Push( const T y)
  58. {
  59.    Node* pNewNode = (Node *)NULL;   // points to new node
  60.  
  61.    pNewNode = new Node;             // allocate new node
  62.    if (pNewNode == (Node *)NULL)
  63.    {
  64.       throw ( MemAlloc());      // throw exception - memory error
  65.    }
  66.  
  67.    pNewNode->datum = y;        // assign data to new node
  68.  
  69.    // add node to stack
  70.    pNewNode->next = pList;
  71.    pList = pNewNode;
  72.  
  73.    pNewNode = (Node *)NULL;
  74.    used++;
  75. }
  76.  
  77.  
  78. template<class T> T
  79. LLStack<T>::Pop()
  80. {
  81.    Node* pTempNode = (Node *)NULL;  // pointer to top node
  82.  
  83.    if ( Empty())
  84.    {
  85.       throw( EmptyExcept());  // throw exception if stack empty
  86.    }
  87.  
  88.    T datum( pList->datum);  // get the data from the top node
  89.  
  90.    // remove top node from stack
  91.    pTempNode = pList;
  92.    pList = pList->next;
  93.    delete pTempNode;
  94.    pTempNode = (Node *)NULL;
  95.  
  96.    used--;                // decrement the number used
  97.    return datum;          // return data
  98. }
  99.  
  100.  
  101.  
  102. template<class T> T
  103. LLStack<T>::OnTop() const
  104. {
  105.    if (Empty())
  106.    {
  107.       throw( EmptyExcept());   // throw exception if stack  empty
  108.    }
  109.    return pList->datum;    // return data
  110. }
  111.  
  112. #endif
  113.