Source of insertNode.cpp


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

  4: // The private method insertNode:
  5: // Adds a given node to the subchain pointed to by subChainPtr
  6: // at a given position. Returns a pointer to the augmented subchain.
  7: template<class ItemType>
  8: Node<ItemType>* LinkedList<ItemType>::insertNode(int position, Node<ItemType>* newNodePtr,
  9:                                                  Node<ItemType>* subChainPtr)
 10: {
 11:    if (position == 1)
 12:    {
 13:       // Insert new node at beginning of subchain
 14:       newNodePtr->setNext(subChainPtr);
 15:       subChainPtr = newNodePtr;
 16:       itemCount++;  // Increase count of entries
 17:    }
 18:    else
 19:    {
 20:       Node<ItemType>* afterPtr = insertNode(position - 1, newNodePtr, subChainPtr->getNext());
 21:       subChainPtr->setNext(afterPtr);
 22:    }  // end if
 23:    
 24:    return subChainPtr;
 25: }  // end insertNode