feat(doubly linked list): implemented clear function

This commit is contained in:
Mineplay 2025-05-02 06:19:22 -05:00
parent 8a8909ab7f
commit 52a81e6aee
3 changed files with 37 additions and 8 deletions

View file

@ -52,6 +52,8 @@ FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinke
FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index);
FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value);
FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list);
bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value);
static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list->size == 0; }

View file

@ -71,26 +71,23 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_dou
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyDoublyLinkedListNode *previous_node = NULL;
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node->next != NULL) {
while (current_node != NULL) {
previous_node = current_node;
current_node = current_node->next;
HallocyError result = hallocy_free(current_node->previous->value);
HallocyError result = hallocy_free(previous_node->value);
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
result = hallocy_free(current_node->previous);
result = hallocy_free(previous_node);
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
if (current_node != NULL) {
hallocy_free(current_node->value);
hallocy_free(current_node);
}
return FLEDASTY_ERROR_NONE;
}
@ -319,6 +316,35 @@ FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list) {
if (current_doubly_linked_list == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyDoublyLinkedListNode *previous_node = NULL;
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node != NULL) {
previous_node = current_node;
current_node = current_node->next;
HallocyError result = hallocy_free(previous_node->value);
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
result = hallocy_free(previous_node);
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
current_doubly_linked_list->size = 0;
current_doubly_linked_list->start = NULL;
current_doubly_linked_list->end = NULL;
return FLEDASTY_ERROR_NONE;
}
bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value) {
if (current_doubly_linked_list == NULL || value == NULL) {
return false;

View file

@ -180,6 +180,7 @@ int main() {
printf("Doubly linked list contains %d\n", insert_value);
}
fledasty_doubly_linked_list_clear(&test_doubly_linked_list);
if (fledasty_doubly_linked_list_is_empty(&test_doubly_linked_list)) {
printf("Doubly linked list is empty\n");
}