home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX17012B.H - LLStack.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
- // %F,15,EX17011B.H%EX17011B.H AStack.H derived array stack
- // %F,15,EX17011B.CPP%EX17011B.CPP AStack.CPP
- // EX17012B.H this file -- LLStack.H 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_EX17012_H
- #define INCL_EX17012_H
-
- #include "EX17010B.H"
- #include "EX17014B.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>
- {
-
- struct Node
- {
- public:
- T datum;
- Node* next;
- };
-
- friend class LLStackItr<T>;
-
-
- Node* pList;
- unsigned long used;
- long lRc;
- 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: 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
- //------------------------------------------------------
- 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 LLStackItr : public Iterator<T>
- {
- LLStack<T>::Node* pNode;
- const T* pArray;
- const LLStack<T>* pStack;
-
- public:
- // FUNCTION: constructor
- // PARAMETERS:
- LLStackItr( const LLStack<T>& stack);
-
- // FUNCTION: operator()
- // RETURN VALUE: the next element
- T&
- operator()();
- };
-
-
- template<class T>
- LLStackItr<T>::LLStackItr( const LLStack<T>& stack)
- : pNode( stack.pList), pStack(&stack)
- {
- }
-
-
- template<class T> T&
- LLStackItr<T>::operator()()
- {
- LLStack<T>::Node* pTempNode = pNode;
-
- if (pStack->Empty()) {
- cout << "Stack Empty: aborting." << endl;
- abort();
- }
- else if (pNode == (LLStack<T>::Node*)NULL)
- {
- cout << "Null Pointer: Aborting." << endl;
- abort();
- }
- pNode = pNode->next;
- return (T&)pTempNode->datum;
- }
-
-
- #endif
-