diff --git a/task3/task3.c b/task3/task3.c index 9bd1e04..a799cd5 100644 --- a/task3/task3.c +++ b/task3/task3.c @@ -24,8 +24,7 @@ void add_node(NODE *list, int val) { if (node == NULL) { return; } - for (; list->next != NULL; list = list->next) - ; + for (; list->next != NULL; list = list->next); list->next = node; } @@ -67,6 +66,43 @@ NODE *reverse_list(NODE *list) { return list; } +void delete_list(NODE* list) { + if(list -> next == NULL) { + free(list); + return; + } + NODE* next_node = list -> next; + while (next_node != NULL) { + free(list); + list = next_node; + next_node = list -> next; + } + free(list); + return; +} + +NODE* list_remove_all(NODE *list, int val) { + NODE* head = list; + NODE* buf; + while (head != NULL && head->val == val) { + buf = head->next; + free(head); + head = buf; + } + if (head == NULL) return head; + list = head; + while (list->next != NULL) { + if (list->next->val == val) { + while(list->next != NULL && list->next->val == val) { + buf = list->next; + list->next = list->next->next; + free(buf); + } + } else list = list->next; + } + return head; +} + int main() { NODE *list = init_node(4); add_node(list, 5); @@ -79,10 +115,17 @@ int main() { add_node(list, 2); add_node(list, 3); add_node(list, 4); + add_node(list, 3); + add_node(list, 3); + add_node(list, 1); print_list(list); printf("\n[%d]\n\n", count_length(list)); list = reverse_list(list); print_list(list); printf("\n[%d]\n\n", count_length(list)); + list = list_remove_all(list, 1); + print_list(list); + printf("\n[%d]\n\n", count_length(list)); + delete_list(list); return 0; -} \ No newline at end of file +}