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

  1. // \EXAMPLES\EX15021.H
  2. //--------------------------------------------------------------
  3. // definition of the class templates ASTACK and ASTACKITR
  4. //--------------------------------------------------------------
  5.  
  6. //  files in this example:
  7. // EX15021.H    this file
  8. // %F,15,EX15022.H%EX15022.H    definition of String class
  9. // %F,15,EX15021.CPP%EX15021.CPP  member functions of Astack
  10. // %F,15,EX1502.CPP%EX1502.CPP   main() - exercise templates
  11.  
  12. //--------------------------------------------------------------
  13. #ifndef ASTACK_H
  14. #define ASTACK_H
  15.  
  16. //--------------------------------------------------------------
  17. #include <iostream.h>
  18.  
  19. //---------------------------------------------------------
  20. // TEMPLATE: class AStack: is used to define a class stack
  21. //                         based on a array of type T
  22. //---------------------------------------------------------
  23. template<class T> class AStack
  24. {
  25.    friend class  AStackItr<T>;    // iterator class
  26.    T             *pArray;         // pointer to the array
  27.    unsigned long size;            // size of the array
  28.    unsigned long free;            // number of free elements
  29. public:
  30.    //-----------------------------------------------------------
  31.    // FUNCTION: Constructor allocates an array of specified size
  32.    // PARAMETERS: long - capacity - the max number of elements
  33.    // RETURN VALUE: none
  34.    //-----------------------------------------------------------
  35.    AStack( unsigned long capacity = 10);
  36.    //-----------------------------------------------------------
  37.    // FUNCTION: Destructor deallocates array
  38.    // PARAMETERS: none
  39.    // RETURN VALUE: none
  40.    //-----------------------------------------------------------
  41.    ~AStack();
  42.    //-----------------------------------------------------------
  43.    // FUNCTION: Push pushes an element of type T onto the stack
  44.    // PARAMETERS: T y is an element of type T
  45.    // RETURN VALUE: none
  46.    //-----------------------------------------------------------
  47.    void Push( const T y);
  48.    //-----------------------------------------------------------
  49.    // FUNCTION: Pop pops an element of type T from the stack
  50.    // PARAMETERS: none
  51.    // RETURN VALUE: returns the top element of the stack
  52.    //-----------------------------------------------------------
  53.    T Pop();
  54.    //-----------------------------------------------------------
  55.    // FUNCTION: Full returns true if stack is full, false
  56.    //                otherwise
  57.    // PARAMETERS: none
  58.    // RETURN VALUE: int
  59.    //----------------------------------------------------------
  60.    inline int Full() const;
  61.    //----------------------------------------------------------
  62.    // FUNCTION: Empty returns true if stack is empty, false
  63.    //                 otherwise
  64.    // PARAMETERS: none
  65.    // RETURN VALUE: int
  66.    //----------------------------------------------------------
  67.    inline int Empty() const;
  68.    //----------------------------------------------------------
  69.    // FUNCTION: NumElem returns the number of elements in stack
  70.    // PARAMETERS: none
  71.    // RETURN VALUE: long the number of elements in the stack
  72.    //------------------------------------------------------
  73.    long NumElem() const;
  74. };
  75.  
  76. //----------------------------------------------------------
  77. //  Inline member functions of ASTACK
  78. //----------------------------------------------------------
  79.  
  80. template<class T> inline int
  81. AStack<T>::Full() const
  82. { // if free is zero return true, false otherwise
  83.    return ( free == 0);
  84. }
  85.  
  86. template<class T> inline int
  87. AStack<T>::Empty() const
  88. { // if free equals size return true, false otherwise
  89.    return ( free == size);
  90. }
  91.  
  92.  
  93. template<class T> inline long
  94. AStack<T>::NumElem() const
  95. { return (size - free);
  96. }
  97.  
  98. //---------------------------------------------------------
  99. // TEMPLATE: class ASTACKITR
  100. //           to iterate through the elements of a stack
  101. //---------------------------------------------------------
  102. template <class T> class AStackItr
  103. { long             index;
  104.    const T*         pArray;
  105.    const AStack<T>* pStack;
  106. public:
  107.    // FUNCTION: constructor
  108.    // PARAMETERS:
  109.    AStackItr( const AStack<T>& stack);
  110.  
  111.    // FUNCTION: operator()
  112.    // RETURN VALUE: the next element
  113.    T& operator()();
  114. };
  115.  
  116. //----------------------------------------------------------
  117. //  Inline member functions of ASTACKITR
  118. //----------------------------------------------------------
  119. template<class T>
  120. AStackItr<T>::AStackItr( const AStack<T>& stack)
  121.  : index(0), pArray( stack.pArray), pStack(&stack)
  122. {
  123. }
  124.  
  125. template<class T> T&
  126. AStackItr<T>::operator()()
  127. { if (pStack->Empty())
  128.   {  cout << "Stack Empty: " << endl;
  129.      abort();
  130.   }
  131.   return (T&)pArray[index++];
  132. }
  133.  
  134. #endif
  135.