home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX17010.H - Stack.H
-
- //----------------------------------------------------------
- // Example of template container classes implenenting stacks.
- // This version includes exception handling and templates.
- //----------------------------------------------------------
- //----------------------------------------------------------
- // 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:
- // EX17010.H this file -- Stack.H base class stack
- // %F,15,EX17011.H%EX17011.H AStack.H derived array stack
- // %F,15,EX17011.CPP%EX17011.CPP AStack.CPP
- // %F,15,EX17012.H%EX17012.H LLStack.H derived linked list stack
- // %F,15,EX17012.CPP%EX17012.CPP LLStack.CPP
- // %F,15,EX17013.H%EX17013.H Exception.H
- // %F,15,EX17014.H%EX17014.H Iterator.H iterator class
- // %F,15,EX1701.CPP%EX1701.CPP main() with exception handling
- // %F,15,EX1701B.CPP%EX1701B.CPP main() without exception handling
- //----------------------------------------------------------
- #ifndef INCL_EX17010_H
- #define INCL_EX17010_H
-
- #include "EX17013.H" // %F,15,EX17013.H%Exception.H
-
- //---------------------------------------------------------
- // TEMPLATE: class Stack: to create a base class for a stack
- // which stores elements of type T
- //
- // COMMENTS: Notice all functions are pure virtuals
- //---------------------------------------------------------
- template<class T> class Stack
- {
- public:
-
- //------------------------------------------------------
- // FUNCTION: Push pushes an element of type T onto the
- // stack
- // PARAMETERS: T y is an element of type T
- // RETURN VALUE: none
- // EXCEPTIONS: throws an int if full
- //------------------------------------------------------
- virtual void
- Push( const T y) = 0;
-
- //------------------------------------------------------
- // FUNCTION: Pop pops an element of type T from the stack
- // PARAMETERS: none
- // RETURN VALUE: returns the top element of the stack
- // EXCEPTIONS: throws an int if empty
- //------------------------------------------------------
- virtual T
- Pop() = 0;
-
- //------------------------------------------------------
- // FUNCTION: Full returns true if stack is full, false
- // otherwise
- // PARAMETERS: none
- // RETURN VALUE: int
- //------------------------------------------------------
- virtual int
- Full() const = 0;
-
- //------------------------------------------------------
- // FUNCTION: Empty returns true if stack is empty, false
- // otherwise
- // PARAMETERS: none
- //
- // RETURN VALUE: int
- //------------------------------------------------------
- virtual int
- Empty() const = 0;
-
- //------------------------------------------------------
- // FUNCTION: OnTop returns the top element of the stack
- // PARAMETERS: none
- // RETURN VALUE: T the top element of the stack
- // EXCEPTIONS: throws an int if empty
- //------------------------------------------------------
- virtual T
- OnTop() const = 0;
-
- //------------------------------------------------------
- // FUNCTION: NumElem returns the number of elements in stack
- // PARAMETERS: none
- // RETURN VALUE: long the number of elements in the stack
- //------------------------------------------------------
- virtual long
- NumElem() const = 0;
-
- class FullExcept : public Exception
- {
- char*
- Name() const
- {
- return "Full Exception";
- }
-
- long
- ErrorNum() const
- {
- return 0x1;
- }
- };
-
- class EmptyExcept : public Exception
- {
- char*
- Name() const
- {
- return "Full Exception";
- }
-
- long
- ErrorNum() const
- {
- return 0x1;
- }
- };
-
- class MemAlloc : public Exception
- {
- char*
- Name() const
- {
- return "Full Exception";
- }
-
- long
- ErrorNum() const
- {
- return 0x1;
- }
- };
- };
-
- #endif
-