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

  1. // \EXAMPLES\EX17011B.CPP    - AStack.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. // EX17011B.CPP     this file -- AStack.CPP
  20. // %F,15,EX17012B.H%EX17012B.H       LLStack.H     derived 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. #include <stdlib.h>
  28.  
  29. #ifndef INCL_EX17011_CPP
  30. #define INCL_EX17011_CPP
  31.  
  32. #include "EX17010B.H"
  33. #include "EX17011B.H"
  34.  
  35. template<class T>
  36. AStack<T>::AStack( unsigned long capacity)
  37.    : size (0), free(0), pArray( (T *)NULL)
  38. {
  39.    pArray = new T[capacity];   // allocate array to store data
  40.    if ( pArray == (T *)NULL)
  41.    {
  42.       cout << "Memory allocation error: Aborting." << endl;
  43.       abort();   // aborts if memory error
  44.    }
  45.    size = free = capacity;  // initialize size and free;
  46. }
  47.  
  48.  
  49. template<class T>
  50. AStack<T>::~AStack()
  51. {
  52.    delete[] pArray;  // deallocate the array
  53. }
  54.  
  55.  
  56. template<class T> void
  57. AStack<T>::Push( const T y)
  58. {
  59.    if ( Full())
  60.    {
  61.       cout << "Stack Full: Aborting." << endl;
  62.       abort(); // abort if full
  63.    }
  64.    pArray[size -free] = y; // store data a next pos
  65.    free--;            // decrement the number of free elements
  66. }
  67.  
  68.  
  69. template<class T> T
  70. AStack<T>::Pop()
  71. {
  72.    if ( Empty())
  73.    {
  74.       cout << "Stack Empty: Aborting." << endl;
  75.       abort(); // abort if empty
  76.    }
  77.    // return top element and remove element from stack
  78.    return pArray[(size - free++) - 1];
  79. }
  80.  
  81.  
  82.  
  83. template<class T> T
  84. AStack<T>::OnTop() const
  85. {
  86.    if (Empty())
  87.    {
  88.       cout << "Stack Empty: Aborting." << endl;
  89.       abort(); // abort if empty
  90.    }
  91.  
  92.    // return top element of stack
  93.    return pArray[size - free - 1];
  94. }
  95.  
  96. #endif
  97.