home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX17012B.CPP - LLStack.CPP
-
- //----------------------------------------------------------
- // 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
- // %F,15,EX17012B.H%EX17012B.H LLStack.H derived linked list stack
- // EX17012B.CPP this file -- 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_CPP
- #define INCL_EX17012_CPP
-
- #include <stdlib.h>
- #include "EX17010B.H"
- #include "EX17012B.H"
-
- template<class T>
- LLStack<T>::LLStack()
- : pList( (Node *)NULL), used( 0)
- {
- }
-
-
- template<class T>
- LLStack<T>::~LLStack()
- {
- Node* pTempNode = (Node *)NULL; // points to node in stack
-
- //stop when pList is null
- while( (pTempNode = pList) != (Node *)NULL)
- {
- pList = pList->next; // remove node from stack
- delete pTempNode; // deallocate node
- }
- pTempNode = (Node *)NULL;
- }
-
-
- template<class T> void
- LLStack<T>::Push( const T y)
- {
- Node* pNewNode = (Node *)NULL; // points to new node
-
- pNewNode = new Node; // allocate new node
- if (pNewNode == (Node *)NULL)
- {
- cout << "Memory Allocation Error: Aborting." << endl;
- abort(); // abort if memory error
- }
-
- pNewNode->datum = y; // assign data to new node
-
- // add node to stack
- pNewNode->next = pList;
- pList = pNewNode;
-
- pNewNode = (Node *)NULL;
- used++;
- }
-
-
- template<class T> T
- LLStack<T>::Pop()
- {
- Node* pTempNode = (Node *)NULL; // pointer to top node
-
- if ( Empty())
- {
- cout << "Stack Empty: Aborting." << endl;
- abort(); // abort if stack is empty
- }
-
- T datum = pList->datum; // get the data from the top node
-
- // remove top node from stack
- pTempNode = pList;
- pList = pList->next;
- delete pTempNode;
- pTempNode = (Node *)NULL;
-
- used--; // decrement the number used
- return datum; // return data
- }
-
-
-
- template<class T> T
- LLStack<T>::OnTop() const
- {
- if (Empty())
- {
- cout << "Stack Empty: Aborting." << endl;
- abort(); // abort if stack is empty
- }
- return pList->datum; // return data
- }
-
- #endif
-