Source of ListingC4-4.cpp


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

  4: // Listing C4-4

  6: template<class ItemType>
  7: bool LinkedList<ItemType>::remove(int position)
  8: {
  9:    bool ableToRemove = (position >= 1) && (position <= itemCount);
 10:    if (ableToRemove)
 11:    {
 12:       if (position == 1)
 13:       {
 14:          // Remove the first node in the chain
 15:          headPtr = headPtr –>getNext();
 16:       }
 17:       else
 18:       {
 19:          // Find node that is before the one to delete
 20:          auto prevPtr = getNodeAt(position – 1);
 21:          
 22:          // Point to node to delete
 23:          auto curPtr = prevPtr –>getNext();
 24:          
 25:          // Disconnect indicated node from chain by connecting the
 26:          // prior node with the one after
 27:          prevPtr –>setNext(curPtr –>getNext());
 28:       } // end if
 29:       
 30:       itemCount––; // Decrease count of entries
 31:    }  // end if
 32:    
 33:    return ableToRemove;
 34: }  // end remove