#include #include #include #include //////////////////////////////////////////////////////////////////////////////// // DO NOT MODIFY THIS STRUCT //////////////////////////////////////////////////////////////////////////////// struct node { struct node *next; int data; }; //////////////////////////////////////////////////////////////////////////////// // extract_evens should create a new linked list `only_evens` that contains // only the even-valued nodes from the original list // // Unfortunately, this code contains a number of errors. // It's your job to fix them, good luck! struct node *extract_evens(struct node *head) { struct node *only_evens = NULL; struct node *current = head; while (current->next != NULL) { struct node next = current->next; current->next = NULL; if (current % 2 == 0) { struct node current_evens = only_evens; while (current_evens == NULL) { current_evens = current_evens->next; } current_evens->next = current; } current = next; } return only_evens; } //////////////////////////////////////////////////////////////////////////////// // DO NOT MODIFY THE CODE BELOW THIS LINE, THIS CODE ALLOWS YOU TO // TAKE IN A LIST OF NUMBERS TO TEST WHETHER YOUR FUNCTION IS WORKING //////////////////////////////////////////////////////////////////////////////// struct node *strings_to_list(int len, char *strings[]); void print_list(struct node *head); //////////////////////////////////////////////////////////////////////////////// // DO NOT CHANGE THIS FUNCTION //////////////////////////////////////////////////////////////////////////////// int main(int argc, char *argv[]) { struct node *head = strings_to_list(argc - 1, &argv[1]); struct node *only_evens = extract_evens(head); if (only_evens == NULL) { printf("No even nodes\n"); } else { print_list(only_evens); } return 0; } //////////////////////////////////////////////////////////////////////////////// // DO NOT CHANGE THIS FUNCTION //////////////////////////////////////////////////////////////////////////////// struct node *strings_to_list(int len, char *strings[]) { struct node *head = NULL; for (int i = len - 1; i >= 0; i--) { struct node *n = malloc(sizeof(struct node)); assert(n != NULL); n->data = atoi(strings[i]); n->next = head; head = n; } return head; } //////////////////////////////////////////////////////////////////////////////// // DO NOT CHANGE THIS FUNCTION //////////////////////////////////////////////////////////////////////////////// void print_list(struct node *head) { struct node *current = head; while (current != NULL) { printf("%d", current->data); if (current->next != NULL) { printf(" "); } current = current->next; } printf("\n"); }