Source of add.cpp


  1: //  Created by Frank M. Carrano and Timothy M. Henry.
  2: //  Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.

  4: template<class ItemType>
  5: bool LinkedBag<ItemType>::add(const ItemType& newEntry)
  6: {
  7:    // Add to beginning of chain: new node references rest of chain;
  8:    // (headPtr is null if chain is empty)        
  9:    Node<ItemType>* nextNodePtr = new Node<ItemType>();
 10:    nextNodePtr->setItem(newEntry);
 11:    nextNodePtr->setNext(headPtr);  // New node points to chain
 12: //   Node<ItemType>* nextNodePtr = new Node<ItemType>(newEntry, headPtr); // alternate code

 14:    headPtr = nextNodePtr;          // New node is now first node
 15:    itemCount++;
 16:    
 17:    return true;                    // The method is always successful
 18: }  // end add