/** * Delete all copies of str from the node list np. */ struct node * np_delete_all (struct node *np, char *str) { // You can't delete from the empty list if (np == NULL) return NULL; // Remove all copies of str from the rest of the list struct node *newcdr = np_delete_all (cdr (np), str); // Is str at the front of the list? If so, delete the first element // and return the modified cdr. if (strcmp (car (np), str) == 0) { free (np); return newcdr; } // Otherwise, just update the cdr else { set_cdr (np, newcdr); return np; } } // np_delete_all /** * Find the last node in a non-null list. */ struct node * np_last_node (struct node *np) { while (np->next != NULL) np = np->next; return np; } // np_last_node int list_delete_all (struct list *lst, char *str) { // Delete the elements lst->head = np_delete_all (lst->head, str); // Update the tail if (lst->head == NULL) lst->tail = NULL; else lst->tail = np_last_node (lst->head); // Update the length lst->length = np_length (lst->head); // And we're done return 1; } // list_delete_all