// Implementation file for linked StackType #include "StackType.h" #include struct NodeType { ItemType info; NodeType* next; }; void StackType::Push(ItemType newItem) // Adds newItem to the top of the stack. // Stack is bounded by size of memory. // Pre: Stack has been initialized. // Post: If stack is full, FullStack exception is thrown; // else newItem is at the top of the stack. { if (IsFull()) throw FullStack(); else { NodeType* location; location = new NodeType; location->info = newItem; location->next = topPtr; topPtr = location; } } void StackType::Pop() // Removes top item from Stack and returns it in item. // Pre: Stack has been initialized. // Post: If stack is empty, EmptyStack exception is thrown; // else top element has been removed. { if (IsEmpty()) throw EmptyStack(); else { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } } ItemType StackType::Top() // Returns a copy of the top item in the stack. // Pre: Stack has been initialized. // Post: If stack is empty, EmptyStack exception is thrown; // else a copy of the top element is returned. { if (IsEmpty()) throw EmptyStack(); else return topPtr->info; } StackType::StackType() // Class constructor. { topPtr = NULL; } bool StackType::IsFull() const // Returns true if there is no room for another ItemType // on the free store; false otherwise. { NodeType* location; try { location = new NodeType; delete location; return false; } catch(std::bad_alloc exception) { return true; } } StackType::~StackType() // Post: stack is empty; all items have been deallocated. { NodeType* tempPtr; while (topPtr != NULL) { tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } } bool StackType::IsEmpty() const { return (topPtr == NULL); }