Question: Please write this function in C: Node *efficient_delete_match(Node *head, int delete_value, int *num_deleted); Expected Output: The function efficient_delete_match should delete any nodes from the list
Please write this function in C:
Node *efficient_delete_match(Node *head, int delete_value, int *num_deleted);





Expected Output:

The function efficient_delete_match should delete any nodes from the list that have a value matching the argument delete_value and it should count the number of deletions that have occurred with num_deleted (a pass-by-reference parameter). The function should return a pointer to the (potentially different) head of the resulting list (which could even be NULL/empty). The function should result in the same resulting list, and same num_deleted value, as the delete_all_matches function that we created in-class. However the delete_all_matches function was inefficient... it works by repeatedly calling delete_first_match until no matches are found. The delete_first_match function traverses the list until the first match is found, and so if we repeatedly call the function, we are traversing the list again and again. Your efficient_delete_match function should traverse the list only once. You can use multiple loops in your code if like, perhaps traversing a portion of the list, and then another portion of the list, but your code should not repeatedly traverse the same elements of the list. The delete_duplicates function may be helpful as inspiration for how to achieve this. As a result, the performance should be vastly improved compared to delete_all_matches, and test code is provided to demonstrate this. #include #include #include #include typedef struct node { int value; struct node *next; } Node; void print_list(Node *head); Node* insert_at_head (Node *head, int new_value); Node *delete_first_match(Node *head, int delete_value, bool *was_deleted); Node *delete_all_matches (Node *head, int delete_value, int *num_deleted); Node *efficient_delete_match(Node *head, int delete_value, int *num_deleted); int main() = = printf(" efficient_delete_match test "); printf("**** "); Node *lists NULL; list8 insert_at_head(list8, 4); list8 insert_at_head(lists, 3); list8 insert_at_head(list8, 4); list8 insert_at_head(list8, 5); list8 insert_at_head(list8, 4); list8 insert_at_head(list8, 4); lists insert_at_head(list8, 7); list8 insert_at_head(list8, 4); lists insert_at_head(list8, 4); printf(" List 8... "); print_list(lists);| int num_deleted = @; list8 efficient_delete_match(list8, 4, &num_deleted); printf(" List 8 after delete... "); print_list(lists); printf("Number of elements deleted: %d ", num_deleted); ******************** = // Test the performance of delete_all_matches vs efficient_delete by // creating Lists with 50000 nodes with values from 9-10 repeated and // deleting 4 from both Lists. printf("Performance test of delete functions "); printf(" ******** "); Node *liste = NULL, "list10 = NULL; for (int i 0; i value new_value; new_node->next NULL; if (head NULL) return new_node; else { new_node->next = head; return new_node; } } void print_list(Node *head) { Node *current; current = head; int i = 0; while (current != NULL) { printf("Node %d: %d ", i, current->value); current = current->next; i++; } } Node *delete_all_matches (Node *head, int delete_value, int *num_deleted) { Node *current = head; bool deleted true; *num_deleted B; int i = 0; = // keep calling delete_first_match until nothing is deleted do { current = delete_first_match(current, delete_value, &deleted); if (deleted) *num_deleted *num_deleted + 1; } while(deleted); return current; } Node *delete_first_match(Node *head, int delete_value, bool *was_deleted) // if the list is empty, no delete occurs, and return empty List if (head NULL) { *was_deleted false; return NULL; } = if (head->value delete_value) { Node *temp head->next; free(head); *was_deleted = true; return temp; } Node *current = head->next; Node *prev = head; == while (current != NULL) { if (current->value delete_value) { prev->next = current->next; free(current); *was_deleted true; return head; } prev current; current = current->next; } // if no deletion occurred | *was_deleted false; return head; efficient_delete_match test List 8... Node : 4 Node 1: 4 Node 2: 7 Node 3: 4 Node 4: 4 Node 5: 5 Node 6: 4 Node 7: 3 Node 8: 4 List 8 after delete... Node : 7 Node 1: 5 Node 2: 3 Number of elements deleted: 6 Performance test of delete functions delete_all_matches: 0.6338185 elements deleted: 5000 efficient_delete_match: 0.0007015 elements deleted: 5000