Source of insert.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 ArrayList<ItemType>::insert(int newPosition, const ItemType& newEntry)
  6: {
  7:    bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1)
  8:                        && (itemCount < maxItems);
  9:    if (ableToInsert)
 10:    {
 11:       // Make room for new entry by shifting all entries at
 12:       // positions from itemCount down to newPosition
 13:       // (no shift if newPosition == itemCount + 1)
 14:       for (int pos = itemCount; pos >= newPosition; pos--)
 15:          items[pos] = items[pos - 1];
 16:          
 17:       // Insert new entry
 18:       items[newPosition] = newEntry;
 19:       itemCount++;  // Increase count of entries
 20:    }  // end if
 21:    
 22:    return ableToInsert;
 23: }  // end insert