feat(doubly linked list): implemented remove value function

This commit is contained in:
Mineplay 2025-05-01 05:36:56 -05:00
parent e2b88338a3
commit 19c9436ae0
4 changed files with 39 additions and 1 deletions

View file

@ -49,6 +49,7 @@ FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLink
FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *after_value, void *value);
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);
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

@ -284,6 +284,42 @@ FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedLi
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value) {
if (current_doubly_linked_list == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, value, current_doubly_linked_list->element_byte_size)) {
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
if (current_node->previous == NULL) {
current_doubly_linked_list->start = current_node->next;
current_doubly_linked_list->start->previous = NULL;
if (current_node->next == NULL) {
current_doubly_linked_list->end = current_node->previous;
}
} else {
current_node->previous->next = current_node->next;
if (current_node->next == NULL) {
current_doubly_linked_list->end = current_node->previous;
} else {
current_node->next->previous = current_node->previous;
}
}
hallocy_free(current_node->next);
hallocy_free(current_node);
current_doubly_linked_list->size -= 1;
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

@ -268,7 +268,6 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link
hallocy_free(current_node->next);
hallocy_free(current_node);
current_linked_list->size -= 1;
return FLEDASTY_ERROR_NONE;
}

View file

@ -155,6 +155,8 @@ int main() {
fledasty_doubly_linked_list_insert_after_value(&test_doubly_linked_list, &insert_at_value, &insert_value);
fledasty_doubly_linked_list_remove_at_index(&test_doubly_linked_list, 2);
remove_value = 0;
fledasty_doubly_linked_list_remove_value(&test_doubly_linked_list, &remove_value);
FledastyDoublyLinkedListNode *test_doubly_linked_list_node = test_doubly_linked_list.start;
for (int i = 0; i < test_doubly_linked_list.size; i += 1) {