home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX15021.CPP
- //--------------------------------------------------------------
- // member functions of the template ASTACK
- // The Microsoft Visual C++ compiler does not support templates.
- // This program does not compile under Microsoft Visual C++.
- //--------------------------------------------------------------
-
- // files in this example:
- // %F,15,EX15021.H%EX15021.H def'n of templates ASTACK and ASTACKITR
- // %F,15,EX15022.H%EX15022.H definition of String class
- // %F,15,EX1502.CPP%EX1502.CPP main() - exercise templates
- // EX15021.CPP this file
-
- //--------------------------------------------------------------
- #ifndef INCL_EX15021_CPP
- #define INCL_EX15021_CPP
-
- //--------------------------------------------------------------
- #include <process.h> // for abort()
- #include "EX15021.H" // %F,15,EX17010B.H%Stack.H
-
- //--------------------------------------------------------------
- // constructor
- //--------------------------------------------------------------
- template<class T>
- AStack<T>::AStack( unsigned long capacity)
- : size (0), free(0), pArray( (T *)NULL)
- { pArray = new T[capacity]; // allocate array used to store data
- if ( pArray == (T *)NULL)
- { cout << "Memory allocation error: " << endl;
- abort(); // aborts if memory error
- }
- size = free = capacity; // initialize size and free;
- }
-
-
- //--------------------------------------------------------------
- // destructor
- //--------------------------------------------------------------
- template<class T>
- AStack<T>::~AStack()
- { delete[] pArray; // deallocate the array
- }
-
-
- //--------------------------------------------------------------
- // function to push (add) item to stack
- //--------------------------------------------------------------
- template<class T> void
- AStack<T>::Push( const T y)
- { // print message if stack full
- if ( Full())
- cout << "Stack Full: " << endl;
- else
- {
- pArray[size -free] = y; // store data a next pos
- free--; // decrement the number of free elements
- }
- }
-
-
- //--------------------------------------------------------------
- // function to pop (remove) item from stack
- //--------------------------------------------------------------
- template<class T> T
- AStack<T>::Pop()
- { // terminate execution if stack empty
- if ( Empty())
- { cout << "Stack Empty: " << endl;
- abort();
- }
- // return top element and remove element from stack
- return pArray[(size - free++) - 1];
- }
-
- #endif
-