How do I implement this function in a sequence class with a linked list toolkit? // void remove_current( ) // Precondition: is_item returns true. // Postcondition: The current item has been removed from the sequence, and the // item after this (if there is one) is now the new current item.
void sequence::remove_current () { if (is_item ==true) node *target_ptr; target_ptr=list_search(head_ptr, target); if (target_ptr == NULL) return false; target_ptr-> set_data(head_ptr-> data()); list_head_remove(head_ptr); --many nodes; return true; } } How do I fix my code above?
To remove a node in a singly linked list you just need to change the target node predecessor's pointer from target node to target node's successor, keeping track of target node's address so that you can then delete it. This will put a bit of a crimp in how you search for the target node, because you'll also need it's predecessor. Draw it out on paper.
Join our real-time social learning platform and learn together with your friends!