Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have these instructions: void remove ( const int index ) . This method accepts an integer. The method should then traverse to and delete
I have these instructions: void removeconst int index This method accepts an integer. The method should then traverse to and
delete the kth node in the list. This is zero based. Make sure to handle all scenarios empty list, one node, first
node, last node, and the general scenario Here is the code I've written, but it is not working correctly: Remove
template
void DoublyLinkedList::removeconst int index
if index
throw std::outofrangeError: Invalid index for removal.";
if thishead
Empty list
throw std::outofrangeError: Cannot remove from an empty list.";
Node temp nullptr;
if index
Removing the first node
temp thishead;
thishead thisheadnext;
if thishead
thisheadprev nullptr;
else
If the list becomes empty after removal, update the tail
thistail nullptr;
else
Node currentNode thishead;
int currentIndex ;
while currentNode && currentIndex index
currentNode currentNodenext;
currentIndex;
if currentNode
throw std::outofrangeError: Index out of bounds.";
temp currentNode;
if currentNodenext
currentNodenextprev currentNodeprev;
else
If removing the last node, update the tail
thistail currentNodeprev;
currentNodeprevnext currentNodenext;
delete temp;
It is failing this test: Test deleting a Kth element that doesn't exist.
dremove;
checkTesttestNum correct, dgetListAsString;
checkTesttestNum correct, dgetListBackwardsAsString; Thanks for any help.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started