// sum_of_positive_negative.c // // Written by YOUR-NAME (YOUR-ZID) // on TODAYS-DATE // // Calculates the sum of positive and negative numbers in an array. #include #include // DO NOT CHANGE THIS STRUCT struct node { struct node *next; int data; }; // MODIFY THIS FUNCTION // sum_of_positive_negative takes in the head of a linked list and calculates // the sum of positive and negative numbers void sum_of_positive_negative( struct node *head, int *positive_sum, int *negative_sum ) { // TODO: Complete this function } //////////////////////////////////////////////////////////////////////// // DO NOT CHANGE THESE PROTOTYPES //////////////////////////////////////////////////////////////////////// struct node *args_to_list(int argc, char *argv[]); void print_list(struct node *head); void free_list(struct node *head); // DO NOT CHANGE THIS FUNCTION int main(int argc, char *argv[]) { struct node *head = args_to_list(argc, argv); printf("LIST: "); print_list(head); int positive_sum; int negative_sum; sum_of_positive_negative(head, &positive_sum, &negative_sum); printf("Positive sum: %d\n", positive_sum); printf("Negative sum: %d\n", negative_sum); free_list(head); return 0; } // DO NOT CHANGE THIS FUNCTION // Converts command-line args to a linked list of integers struct node *args_to_list(int argc, char *argv[]) { struct node *head = NULL; for (int i = argc - 1; i >= 1; i--) { struct node *new = malloc(sizeof(struct node)); new->data = atoi(argv[i]); new->next = head; head = new; } return head; } // DO NOT CHANGE THIS FUNCTION // Prints a linked list of integers void print_list(struct node *head) { struct node *curr = head; if (curr == NULL) { printf("NULL\n"); return; } while (curr != NULL) { printf("%d", curr->data); if (curr->next != NULL) { printf(" -> "); } curr = curr->next; } printf(" -> NULL\n"); } void free_list(struct node *head) { struct node *curr = head; while (curr != NULL) { struct node *to_free = curr; curr = curr->next; free(to_free); } }