Implement this function: void list_piece(const node* start_ptr, const node* end_
ID: 3859618 • Letter: I
Question
Implement this function: void list_piece(const node* start_ptr, const node* end_ptr, node* & head_ptr, node* & tail_ptr) //Precondition: start_ptr and end_ptr are pointers to nodes on the same //linked list, with the start_ptr node at or before the end_ptr node. //Postcondition: head_ptr and tail_ptr are the head and tail pointers //for a new list that contains the items from start_ptr up to but not //including end_ptr. The end_ptr may also be NULL, in which case the //new list contains elements from start_ptr to the end of the list.Explanation / Answer
void list_piece(const node* start_ptr, const node* end_ptr, node*& head_ptr, node*& tail_ptr) { head_ptr = NULL; tail_ptr = NULL; // Handle the case of the empty list. if (start_ptr == NULL) { return; } // Make the head node for the newly created list, and put data in it. list_head_insert(head_ptr, start_ptr->data( )); tail_ptr = head_ptr; // Copy the rest of the nodes one at a time, adding at the tail of new list. start_ptr = start_ptr->link( ); while (start_ptr != end_ptr) { list_insert(tail_ptr, start_ptr->data( )); tail_ptr = tail_ptr->link( ); start_ptr = start_ptr->link( ); } return; }