home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX17011B.H - AStack.H
-
- //----------------------------------------------------------
- // Example of template container classes implenenting stacks.
- // This version includes templates but no exception handling.
- //----------------------------------------------------------
- //----------------------------------------------------------
- // Exception handling is supported only by
- // the IBM C++ Set/2 compiler
- // Templates are not supported by
- // the Microcoft Visual C++ compiler
- // Two versions of this program are included:
- // EX1701I.EXE - for IBM CSet II
- // EX1701B.EXE - for Borland Turbo C++
- //----------------------------------------------------------
- // Files in this example:
- // %F,15,EX17010B.H%EX17010B.H Stack.H base class stack
- // EX17011B.H this file -- AStack.H array stack
- // %F,15,EX17011B.CPP%EX17011B.CPP AStack.CPP
- // %F,15,EX17012B.H%EX17012B.H LLStack.H derived linked list stack
- // %F,15,EX17012B.CPP%EX17012B.CPP LLStack.CPP
- // %F,15,EX17013B.H%EX17013B.H Exception.H
- // %F,15,EX17014B.H%EX17014B.H Iterator.H iterator class
- // %F,15,EX1701B.CPP%EX1701B.CPP main() without exception handling
- // %F,15,EX1701.CPP%EX1701.CPP main() with exception handling
- //----------------------------------------------------------
- #ifndef INCL_EX17011_H
- #define INCL_EX17011_H
-
- #include <iostream.h>
- #include "EX17010B.H"
- #include "EX17014B.H"
-
- //---------------------------------------------------------
- // TEMPLATE: class AStack: is used to define a class stack
- // based on a array list which
- // stores elements of type T
- //
- // COMMENTS: Notice all functions are pure virtuals
- //---------------------------------------------------------
- template<class T> class AStack : public Stack<T>
- {
- friend class AStackItr<T>;
-
- unsigned long free; // number of free elements in the array
- unsigned long size; // size of the array
- T *pArray; // pointer to the array
-
- public:
- //------------------------------------------------------
- // FUNCTION: Constructor allocates an array of specified
- // size
- // PARAMETERS: long capacity the max number of element
- // required
- // RETURN VALUE: none
- // EXCEPTIONS THROWN: int if memory allocation error
- //------------------------------------------------------
- AStack( unsigned long capacity = 10);
-
- //------------------------------------------------------
- // FUNCTION: Destructor deallocates array
- // PARAMETERS: none
- // RETURN VALUE: none
- //------------------------------------------------------
- ~AStack();
-
- //------------------------------------------------------
- // FUNCTION: Push pushes an element of type T onto the
- // stack
- // PARAMETERS: T y is an element of type T
- // RETURN VALUE: none
- // EXCEPTIONS: aborts if full
- //------------------------------------------------------
- void
- Push( const T y);
-
- //------------------------------------------------------
- // FUNCTION: Pop pops an element of type T from the stack
- // PARAMETERS: none
- // RETURN VALUE: returns the top element of the stack
- // EXCEPTIONS: aborts if empty
- //------------------------------------------------------
- T
- Pop();
-
- //------------------------------------------------------
- // FUNCTION: Full returns true if stack is full, false
- // otherwise
- // PARAMETERS: none
- // RETURN VALUE: int
- //------------------------------------------------------
- inline int
- Full() const;
-
- //------------------------------------------------------
- // FUNCTION: Empty returns true if stack is empty, false
- // otherwise
- // PARAMETERS: none
- //
- // RETURN VALUE: int
- //------------------------------------------------------
- inline int
- Empty() const;
-
- //------------------------------------------------------
- // FUNCTION: OnTop returns the top element of the stack
- // PARAMETERS: none
- // RETURN VALUE: T the top element of the stack
- // EXCEPTIONS: aborts if empty
- //------------------------------------------------------
- T
- OnTop() const;
-
- //------------------------------------------------------
- // FUNCTION: NumElem returns number of elements in stack
- // PARAMETERS: none
- // RETURN VALUE: long the number of elements in the stack
- //------------------------------------------------------
- long
- NumElem() const;
- };
-
-
- template<class T> inline int
- AStack<T>::Full() const
- {
- // if free is zero return true, false otherwise
- return ( free == 0);
- }
-
- template<class T> inline int
- AStack<T>::Empty() const
- {
- // if free equals size return true, false otherwise
- return ( free == size);
- }
-
-
- template<class T> inline long
- AStack<T>::NumElem() const
- {
- return (size - free);
- }
-
-
- //
- // Use to iterate through the elements of a stack
- //
- template <class T> class AStackItr : public Iterator<T>
- {
- long index;
- const T* pArray;
- const AStack<T>* pStack;
-
- public:
- // FUNCTION: constructor
- // PARAMETERS:
- AStackItr( const AStack<T>& stack);
-
- // FUNCTION: operator()
- // RETURN VALUE: the next element
- T&
- operator()();
- };
-
-
- template<class T>
- AStackItr<T>::AStackItr( const AStack<T>& stack)
- : index(0), pArray( stack.pArray), pStack(&stack)
- {
- }
-
-
- template<class T> T&
- AStackItr<T>::operator()()
- {
- if (pStack->Empty())
- {
- cout << "Stack Empty: Aborting." << endl;
- abort();
- }
- return (T&)pArray[index++];
- }
-
-
- #endif
-