Source of copyConstructor.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: LinkedSortedList<ItemType>::LinkedSortedList(const LinkedSortedList<ItemType>& aList)
  6: {
  7:         headPtr = copyChain(aList.headPtr);
  8:    itemCount = aList.itemCount;
  9: }  // end copy constructor

 11: template<class ItemType>
 12: auto LinkedSortedList<ItemType>::
 13:      copyChain(const std::shared_ptr<Node<ItemType>>& origChainPtr)
 14: {
 15:    std::shared_ptr<Node<ItemType>> copiedChainPtr; // Initial value is nullptr
 16:    if (origChainPtr != nullptr)
 17:    {
 18:       // Build new chain from given one
 19:       // Create new node with the current item
 20:       copiedChainPtr = std::make_shared<Node<ItemType>>(origChainPtr–>getItem());
 21:       
 22:       // Make the node point to the rest of the chain
 23:       copiedChainPtr–>setNext(copyChain(origChainPtr–>getNext()));
 24:    } // end if
 25:    
 26:    return copiedChainPtr;
 27: } // end copyChain