Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Doubly-Linked List [10 points] Consider the following implementation of the erase() function for a doubly-linked list (DList). It deletes the node pointed to by

image text in transcribed

1. Doubly-Linked List [10 points] Consider the following implementation of the erase() function for a doubly-linked list (DList). It deletes the node pointed to by the iterator passed in. void Dlist: :erase (pist::iterator it) 1/ zxBook2.7 (they call it remove) DListNode * succ= it.node->next; // successor node RistNaded - ismaede->erei // predecessor node if (suse NULL) if (eredNULL) pred->next = succ; head - sus: // head is following node tail-pxed:// tail is previous node delete it.ode /I delete the node; // iterator is now invalid, caller should not use it again In its current form, the erase function makes the iterator passed as an argument illegal to use later, because it will still be pointing to the erased node. But, we can fix this by passing a reference and updating the iterator to point to the previous node in the list. Modify the erase function to accept a reference to DList iterator& instead. After erasing the element, change the iterator to point to the previous element in the DList, if it exists. Otherwise, set the iterator node to nullptr

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions