/* * MyArrayList is our example of implementing a basic data structure. */ #ifndef _my_MyArrayList_h #define _my_MyArrayList_h #include #include #include using namespace std; /* * An MyArrayList is an ordered collection of integers stored and accessed * with 0-based integer indexes, using an array as the internal representation. */ template class MyArrayList { public: /* * Constructs a new empty list. */ MyArrayList(); MyArrayList(const MyArrayList&); MyArrayList& operator =(const MyArrayList& other); /* * This destructor frees the memory that was allocated internally by the list. */ ~MyArrayList(); /* * Appends the given value to the end of the list. */ void add(T value); /* * Removes all values from the list. */ void clear(); /* * Returns the value at the given 0-based index of the list. */ int get(int index) const; /* * Adds the given value just before the given 0-based index in the list, * shifting subsequent elements right as necessary to make room. */ void insert(int index, T value); /* * Returns true if there are no elements in the list. */ bool isEmpty() const; /* * Removes the element at the given 0-based index from the list, * shifting subsequent elements left as necessary to cover its slot. */ void remove(int index); /* * Stores the given value at the given 0-based index in the list. */ void set(int index, T value); /* * Returns the number of elements in the list. */ int size() const; /* * Returns a string representation of the list such as "[42, 3, 17]". */ string toString() const; private: // member variables inside each list object; // we precede them with "m_" just as a stylistic convention // to make them easier to identify in the .cpp code int m_size = 0; // number of elements added int m_capacity = 0; // length of array T* m_elements = nullptr; // array of elements /* * This private helper resizes the list's internal array buffer if necessary * to accommodate additional elements. */ void checkResize(); void checkIndex(int i, int min, int max)const; }; #include "MyArrayList.cpp" #endif // _MyArrayList_h