// Created by Frank M. Carrano and Tim Henry. // Copyright (c) 2013 __Pearson Education__. All rights reserved. /** Listing 7-4. @file LinkedStack.cpp */ #include // For assert #include "LinkedStack.h" // Header file template LinkedStack::LinkedStack() : topPtr(nullptr) { } // end default constructor template LinkedStack::LinkedStack(const LinkedStack& aStack) { // Point to nodes in original chain Node* origChainPtr = aStack.topPtr; if (origChainPtr == nullptr) topPtr = nullptr; // Original stack is empty else { // Copy first node topPtr = new Node(); topPtr->setItem(origChainPtr->getItem()); // Point to last node in new chain Node* newChainPtr = topPtr; // Advance original-chain pointer origChainPtr = origChainPtr->getNext(); // Copy remaining nodes while (origChainPtr != nullptr) { // Get next item from original chain ItemType nextItem = origChainPtr->getItem(); // Create a new node containing the next item Node* newNodePtr = new Node(nextItem); // Link new node to end of new chain newChainPtr->setNext(newNodePtr); // Advance pointer to new last node newChainPtr = newChainPtr->getNext(); // Advance original-chain pointer origChainPtr = origChainPtr->getNext(); } // end while newChainPtr->setNext(nullptr); // Flag end of chain } // end if } // end copy constructor template LinkedStack::~LinkedStack() { // Pop until stack is empty while (!isEmpty()) pop(); } // end destructor template bool LinkedStack::isEmpty() const { return topPtr == nullptr; } // end isEmpty template bool LinkedStack::push(const ItemType& newItem) { Node* newNodePtr = new Node(newItem, topPtr); topPtr = newNodePtr; newNodePtr = nullptr; return true; } // end push template bool LinkedStack::pop() { bool result = false; if (!isEmpty()) { // Stack is not empty; delete top Node* nodeToDeletePtr = topPtr; topPtr = topPtr->getNext(); // Return deleted node to system nodeToDeletePtr->setNext(nullptr); delete nodeToDeletePtr; nodeToDeletePtr = nullptr; result = true; } // end if return result; } // end pop template ItemType LinkedStack::peek() const { assert(!isEmpty()); // Enforce precondition // Stack is not empty; return top return topPtr->getItem(); } // end getTop // End of implementation file.