text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

StackP.cpp

Go to the documentation of this file.
00001 
00017 #include <cstddef>   // for NULL
00018 #include <new>       // for bad_alloc
00019 #include "StackP.h"  // header file
00020 
00021 using namespace std;
00022 
00023 Stack::Stack() : topPtr(NULL)
00024 {
00025 }  // end default constructor
00026 
00027 Stack::Stack(const Stack& aStack)
00028 {
00029    if (aStack.topPtr == NULL)
00030       topPtr = NULL;  // original list is empty
00031 
00032    else
00033    {  // copy first node
00034       topPtr = new StackNode;
00035       topPtr->item = aStack.topPtr->item;
00036 
00037       // copy rest of list
00038       StackNode *newPtr = topPtr;    // new list pointer
00039       for (StackNode *origPtr = aStack.topPtr->next;
00040       origPtr != NULL;
00041       origPtr = origPtr->next)
00042       {  newPtr->next = new StackNode;
00043          newPtr = newPtr->next;
00044     newPtr->item = origPtr->item;
00045       }  // end for
00046 
00047       newPtr->next = NULL;
00048    }  // end if
00049 }  // end copy constructor
00050 
00051 Stack::~Stack()
00052 {
00053    // pop until stack is empty
00054    while (!isEmpty())
00055       pop();
00056    // Assertion: topPtr == NULL
00057 }  // end destructor
00058 
00059 bool Stack::isEmpty() const
00060 {
00061    return topPtr == NULL;
00062 }  // end isEmpty
00063 
00064 void Stack::push(const StackItemType& newItem)
00065             throw(StackException)
00066 {
00067    // create a new node
00068    try
00069    {
00070       StackNode *newPtr = new StackNode;
00071 
00072       // set data portion  of new node
00073       newPtr->item = newItem;
00074 
00075       // insert the new node
00076       newPtr->next = topPtr;
00077       topPtr = newPtr;
00078    }
00079    catch (bad_alloc e)
00080    {
00081       throw StackException(
00082     "StackException: push cannot allocate memory.");
00083    }  // try
00084 }  // end push
00085 
00086 void Stack::pop() throw(StackException)
00087 {
00088    if (isEmpty())
00089       throw StackException("StackException: stack empty on pop");
00090    else
00091    {  // stack is not empty; delete top
00092       StackNode *temp = topPtr;
00093       topPtr = topPtr->next;
00094       // return deleted node to system
00095       temp->next = NULL;  // safeguard
00096       delete temp;
00097    }  // end if
00098 }  // end pop
00099 
00100 void Stack::pop(StackItemType& stackTop) throw(StackException)
00101 {
00102    if (isEmpty())
00103       throw StackException("StackException: stack empty on pop");
00104    else
00105    {  // stack is not empty; retrieve and delete top
00106       stackTop = topPtr->item;
00107       StackNode *temp = topPtr;
00108       topPtr = topPtr->next;
00109 
00110       // return deleted node to system
00111       temp->next = NULL;  // safeguard
00112       delete temp;
00113    }  // end if
00114 }  // end pop
00115 
00116 void Stack::getTop(StackItemType& stackTop) const throw(StackException)
00117 {
00118    if (isEmpty())
00119       throw StackException("StackException: stack empty on getTop");
00120    else
00121       // stack is not empty; retrieve top
00122       stackTop = topPtr->item;
00123 }  // end getTop
00124 // End of implementation file.

Generated on Sun Aug 27 16:41:24 2006 for AWLogo by  doxygen 1.4.6