int list_delete_all (struct list *lst, char *str) { struct node *garbage; // Something we want to delte struct node *current; // For iterating lists struct node *previous; // Also for iterating lists // Special case: The value is at the head while ((lst->head != NULL) && (strcmp (car (lst->head), str) == 0)) { garbage = lst->head; lst->head = cdr (lst->head); free (garbage); --(lst->length); } // while // At this point, we know that the head of the list does not contain // str. However, it may be empty. if (lst->head == NULL) { lst->tail = NULL; lst->length = 0; return 1; } // If we reach this point, the list is nonempty, and the head does not // contain str. previous = lst->head; current = lst->head->next; // Keep going until we reach the end of the list while (current != NULL) { // If the current element contains str if (strcmp (car (current), str) == 0) { // Delete it garbage = current; current = cdr (current); free (garbage); set_cdr (previous, current); --(lst->length); } // If the current element does not contain str, go on else { previous = current; current = cdr (current); } } // while // Update the tail lst->tail = previous; // And we're done. return 1; } // list_delete_all