Source of Listing9-1.h


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

  4: /** ADT list: Array-based implementation.
  5:     Listing 9-1.
  6:     @file ArrayList.h */

  8: #ifndef ARRAY_LIST_
  9: #define ARRAY_LIST_

 11: #include "ListInterface.h"
 12: #include "PrecondViolatedExcept.h"

 14: template<class ItemType>
 15: class ArrayList : public ListInterface<ItemType>
 16: {
 17: private:
 18:         static const int DEFAULT_CAPACITY = 100; // Default capacity of the list
 19:         ItemType items[DEFAULT_CAPACITY + 1];    // Array of list items (ignore items[0]
 20:    int itemCount;                           // Current count of list items
 21:    int maxItems;                            // Maximum capacity of the list
 22:    
 23: public:
 24:    ArrayList();
 25:    // Copy constructor and destructor are supplied by compiler
 26:    
 27:    bool isEmpty() const;
 28:    int getLength() const;
 29:    bool insert(int newPosition, const ItemType& newEntry);
 30:    bool remove(int position);
 31:    void clear();
 32:    
 33:    /** @throw  PrecondViolatedExcept if position < 1 or position > getLength(). */
 34:    ItemType getEntry(int position) const throw(PrecondViolatedExcept);

 36:    /** @throw  PrecondViolatedExcept if position < 1 or position > getLength(). */
 37:    void setEntry(int position, const ItemType& newEntry) 
 38:                                throw(PrecondViolatedExcept);
 39: }; // end ArrayList

 41: #include "ArrayList.cpp"
 42: #endif