home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX17012.H - LLStack.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:
- // %F,15,EX17010.H%EX17010.H Stack.H base class stack
- // %F,15,EX17011.H%EX17011.H AStack.H derived array stack
- // %F,15,EX17011.CPP%EX17011.CPP AStack.CPP
- // EX17012.H this file -- LLStack.H 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
- //----------------------------------------------------------
- // \EXAMPLES\EX17012.H - LLStack.H
-
- #ifndef INCL_EX17012_H
- #define INCL_EX17012_H
-
- #include "EX17010.H"
- #include "EX17014.H"
-
- //---------------------------------------------------------
- // TEMPLATE: class LLStack: is used to define a class stack
- // based on a linked list which
- // stores elements of type T
- //
- // COMMENTS: Notice all functions are pure virtuals
- //---------------------------------------------------------
- template<class T> class LLStack : public Stack<T>
- {
- friend class LLStackIter<T>;
-
- struct Node
- {
- T datum;
- Node* next;
- };
-
- Node* pList;
-
- unsigned long used;
-
- public:
- //------------------------------------------------------
- // FUNCTION: Constructor initiallizes member data
- // PARAMETERS: none
- // RETURN VALUE: none
- //------------------------------------------------------
- LLStack();
-
- //------------------------------------------------------
- // FUNCTION: Destructor deallocates nodes
- // PARAMETERS: none
- // RETURN VALUE: none
- //------------------------------------------------------
- ~LLStack();
-
- //------------------------------------------------------
- // 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
- //------------------------------------------------------
- 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: throws an int 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: throws an int 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
- //------------------------------------------------------
- virtual long
- NumElem() const;
- };
-
-
- template<class T> inline long
- LLStack<T>::NumElem() const
- {
- return (used);
- }
-
-
- template<class T> inline int
- LLStack<T>::Full() const
- {
- // return false, never full
- return (0);
- }
-
-
- template<class T> inline int
- LLStack<T>::Empty() const
- {
- // if pList is NULL Stack is empty
- return ( pList == (Node *)NULL);
- }
-
-
- //
- // Use to iterate through the elements of a stack
- //
- template <class T> class LLStackIter : public Iterator<T>
- {
- LLStack<T>::Node* pNode;
- const T* pArray;
- const LLStack<T>* pStack;
-
- public:
- // FUNCTION: constructor
- // PARAMETERS:
- LLStackIter( const LLStack<T>& stack);
-
- // FUNCTION: operator()
- // RETURN VALUE: the next element
- T&
- operator()();
- };
-
-
- template<class T>
- LLStackIter<T>::LLStackIter( const LLStack<T>& stack)
- : pNode( stack.pList), pStack(&stack)
- {
- }
-
-
- template<class T> T&
- LLStackIter<T>::operator()()
- {
- LLStack<T>::Node* pTempNode = pNode;
-
- if (pStack->Empty()) {
- throw( LLStack<T>::EmptyExcept());
- }
- else if (pNode == (LLStack<T>::Node*)NULL)
- {
- throw( LLStack<T>::EmptyExcept());
- }
- pNode = pNode->next;
- return (T&)pTempNode->datum;
- }
-
-
- #endif
-