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

Implement the following function as a new function for the linked list toolkit.

ID: 3816412 • Letter: I

Question

Implement the following function as a new function for the linked list toolkit. (Use the usual node definition with member variables called data and link.)

void list_tail_insert(node* head_ptr, const node::value_type& entry);

// Precondition: head_ptr is the head pointer of a non-empty linked list.

// Postcondition: A new node has been added at the tail end

// of the list. The data in the new node is taken from the parameter called entry.

Explanation / Answer

                   #include #include //provide NULL and size_t #include "Node.h" #include #include using namespace std; size_t Node::list_length(const Node* head_ptr) { const Node* cursor; size_t answer; answer = 0; for(cursor = head_ptr; cursor != NULL; cursor = cursor->link()) { answer++; } return answer; } void list_head_insert(Node*& head_ptr, const Node::value_type& entry) { head_ptr = new Node(entry, head_ptr); } void list_insert(Node* previous_ptr, const Node::value_type& entry) { Node *insert_ptr; insert_ptr = new Node(entry, previous_ptr->link()); previous_ptr->setLink(insert_ptr); } Node* list_search(Node* head_ptr,const Node::value_type& target) { Node* cursor; for(cursor = head_ptr; cursor != NULL; cursor=cursor->link()) { if(target == cursor->data()) { return cursor; } } return NULL; } const Node* list_search(const Node* head_ptr, const Node::value_type& target) { const Node *cursor; for(cursor = head_ptr; cursor != NULL; cursor = cursor->link()) { if(target == cursor->data()) { return cursor; } } return NULL; } Node* Node::list_locate(Node* head_ptr, size_t position) { Node *cursor; size_t i; assert(0link(); previous_ptr->setLink(remove_ptr->link()); delete remove_ptr; } void list_clear(Node*& head_ptr) { while(head_ptr != NULL) { list_head_remove(head_ptr); } } void list_copy(const Node* source_ptr, Node*& head_ptr, Node*& tail_ptr) { head_ptr = NULL; tail_ptr = NULL; //case (empty) if(source_ptr == NULL) { return; } //case(newly created) list_head_insert(head_ptr, source_ptr->data()); tail_ptr = head_ptr; //copy the rest of the nodes source_ptr = source_ptr->link(); while(source_ptr != NULL) { list_insert(tail_ptr, source_ptr->data()); tail_ptr = tail_ptr->link(); source_ptr = source_ptr->link(); } } void list_tail_insert(Node*& head_ptr, const Node::value_type& entry) { Node* cursor; Node* previous; if(head_ptr == NULL) { return; } previous = head_ptr; for(cursor = head_ptr; cursor != NULL; cursor = cursor->link()) { previous = cursor; } previous->setLink(new Node(entry,NULL)); printf("added successfully"); //return head_ptr; } void item_remove(Node*& head_ptr, const Node::value_type& entry) { Node* cursor; Node* previous; for(cursor = head_ptr; cursor != NULL; cursor = cursor->link()) { if(entry == cursor->data()) { previous->setLink(cursor->link()); } previous = cursor; } } bool isThere(Node* head_ptr,const Node::value_type& entry) { Node* cursor; for(cursor = head_ptr; cursor != NULL; cursor = cursor->link()) { if(entry == cursor->data()) { return true; } } return false; }