Question: this is an incorrect implementation that ignores the position desired and always inserts at the beginning of the list. As a result, note how the

this is an incorrect implementation that ignores the position desired and always inserts at the beginning of the list. As a result, note how the test result for the entry at position 4 fails! Your job is to fix this function so it inserts in the correct position in the list.

template bool LinkedList::insert(int newPosition, const ItemType& newEntry) { bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1); if (ableToInsert) { // Create a new node containing the new entry Node* newNodePtr = new Node(newEntry);

// This implementation ignores newPosition, and always put the new // item at the beginning of the list. // Your assignment is to correctly insert the item into newPosition newNodePtr->setNext(headPtr); headPtr = newNodePtr;

itemCount++; // Increase count of entries } // end if return ableToInsert; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!