exam task

This commit is contained in:
Inex Code 2020-05-21 10:58:11 +03:00
parent 0233e876c4
commit 7cb07f66ef
1 changed files with 46 additions and 3 deletions

View File

@ -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;
}
}