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

  1. // \EXAMPLES\EX17012B.CPP    - LLStack.CPP
  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. // %F,15,EX17012B.H%EX17012B.H       LLStack.H     derived linked list stack
  21. // EX17012B.CPP     this file -- 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_CPP
  28. #define INCL_EX17012_CPP
  29.  
  30. #include <stdlib.h>
  31. #include "EX17010B.H"
  32. #include "EX17012B.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.      cout << "Memory Allocation Error: Aborting." << endl;
  65.      abort();     // abort if memory error
  66.    }
  67.  
  68.    pNewNode->datum = y;        // assign data to new node
  69.  
  70.    // add node to stack
  71.    pNewNode->next = pList;
  72.    pList = pNewNode;
  73.  
  74.    pNewNode = (Node *)NULL;
  75.    used++;
  76. }
  77.  
  78.  
  79. template<class T> T
  80. LLStack<T>::Pop()
  81. {
  82.    Node* pTempNode = (Node *)NULL;  // pointer to top node
  83.  
  84.    if ( Empty())
  85.    {
  86.       cout << "Stack Empty: Aborting." << endl;
  87.       abort();   // abort if stack is empty
  88.    }
  89.  
  90.    T datum = pList->datum;  // get the data from the top node
  91.  
  92.    // remove top node from stack
  93.    pTempNode = pList;
  94.    pList = pList->next;
  95.    delete pTempNode;
  96.    pTempNode = (Node *)NULL;
  97.  
  98.    used--;                // decrement the number used
  99.    return datum;          // return data
  100. }
  101.  
  102.  
  103.  
  104. template<class T> T
  105. LLStack<T>::OnTop() const
  106. {
  107.    if (Empty())
  108.    {
  109.       cout << "Stack Empty: Aborting." << endl;
  110.       abort();   // abort if stack is empty
  111.    }
  112.    return pList->datum;    // return data
  113. }
  114.  
  115. #endif
  116.