Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a helper function for the LinkedList class : void LinkedList::removeAf

ID: 3800203 • Letter: I

Question

Implement a helper function for the LinkedList class :

void LinkedList::removeAfter(Node * pre)

The job of this function is to remove the node right after pre. (You can assume that pre and pre->next are not null.) The function should also decrement length since the linked list will be one node shorter than before the function was called.

Remember: to remove the node after pre you need to save the node's address in a temp variable , then set pre->next to temp->next. Finally you should delete temp and decrement length.

The function will be a helper function for removeValue and removeAtPosition. That means you are allowed to call it when you implement those functions.

Explanation / Answer

As you described in question that pre and pre->next are not null so I am discarding two conditions in function.

void LinkedList::removeAfter(Node *pre) {
if (pre->next->next == NULL) {
Node *temp = pre->next;
pre->next = NULL;
delete temp;
}
else {
Node *temp = pre->next;
pre->next = temp->next;
delete temp;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote