April 2018
Intermediate to advanced
322 pages
6h 57m
English
The Remove() method will iterate all List elements, and then assign the new array to hold new List elements. It will skip the selected index to remove the item. The implementation should be as follows:
void List::Remove(int index){ // Check if the index is out of bound if(index < 0 || index > m_count) return; // Copy the current array int * oldArray = m_items; // Decrease the array length m_count--; // Initialize a new array m_items = new int[m_count]; // Fill the new array // and remove the selected index for(int i=0, j=0; i < m_count; ++i, ++j) { if(index == j) { ++j; } m_items[i] = oldArray[j]; } // Remove copied array delete [] oldArray;}
Similar to the Insert() method, the complexity of the Remove() ...