feat(linked list): implemented clear function

This commit is contained in:
Mineplay 2025-05-02 06:13:04 -05:00
parent 3c8cc19cab
commit 8a8909ab7f
6 changed files with 44 additions and 13 deletions

View file

@ -47,12 +47,12 @@ FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table,
FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table);
FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value);
void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key);
void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void *key);
FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key);
FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table);
bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key);
static inline bool fledasty_hash_table_is_empty(FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; }
bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key);
static inline bool fledasty_hash_table_is_empty(const FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; }
#endif

View file

@ -51,6 +51,8 @@ FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *curren
FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_linked_list, const size_t index);
FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_linked_list, void *value);
FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list);
bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value);
static inline bool fledasty_linked_list_is_empty(const FledastyLinkedList *current_linked_list) { return current_linked_list->size == 0; }

View file

@ -76,12 +76,12 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_dou
current_node = current_node->next;
HallocyError result = hallocy_free(current_node->previous->value);
if (result == HALLOCY_ERROR_NONE) {
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
result = hallocy_free(current_node->previous);
if (result == HALLOCY_ERROR_NONE) {
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
@ -312,7 +312,7 @@ FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList
}
}
hallocy_free(current_node->next);
hallocy_free(current_node->value);
hallocy_free(current_node);
current_doubly_linked_list->size -= 1;

View file

@ -107,7 +107,7 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table,
return FLEDASTY_ERROR_NONE;
}
void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key) {
void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void *key) {
if (current_hash_table == NULL || key == NULL) {
return NULL;
}
@ -181,7 +181,7 @@ FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table) {
return FLEDASTY_ERROR_NONE;
}
bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key) {
bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key) {
if (current_hash_table == NULL || key == NULL) {
return false;
}

View file

@ -74,12 +74,12 @@ FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_li
current_node = current_node->next;
HallocyError result = hallocy_free(previous_node->value);
if (result == HALLOCY_ERROR_NONE) {
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
result = hallocy_free(previous_node);
if (result == HALLOCY_ERROR_NONE) {
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
@ -265,13 +265,42 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link
previous_node->next = current_node->next;
}
hallocy_free(current_node->next);
hallocy_free(current_node->value);
hallocy_free(current_node);
current_linked_list->size -= 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list) {
if (current_linked_list == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyLinkedListNode *previous_node = NULL;
FledastyLinkedListNode *current_node = current_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_linked_list->size = 0;
current_linked_list->start = NULL;
current_linked_list->end = NULL;
return FLEDASTY_ERROR_NONE;
}
bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value) {
if (current_linked_list == NULL || value == NULL) {
return false;

View file

@ -139,15 +139,15 @@ int main() {
printf("Linked list contains %d\n", insert_value);
}
fledasty_linked_list_clear(&test_linked_list);
if (fledasty_linked_list_is_empty(&test_linked_list)) {
printf("Linked list is empty\n");
}
fledasty_linked_list_destroy(&test_linked_list);
FledastyDoublyLinkedList test_doubly_linked_list;
fledasty_doubly_linked_list_initialize(&test_doubly_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
for (int i = 0; i < 10; i += 1) {
fledasty_doubly_linked_list_append(&test_doubly_linked_list, &i);
}