perf(linked list): made same improvements to searching for value as in doubly linked list

This commit is contained in:
Mineplay 2025-05-22 17:58:01 -05:00
parent 31e9953bf7
commit b43b5c550d

View file

@ -199,15 +199,8 @@ FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *curre
FledastyLinkedListNode *previous_node = NULL;
FledastyLinkedListNode *current_node = current_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, before_value, current_linked_list->element_byte_size)) {
previous_node = current_node;
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
while (current_node != NULL) {
if (hallocy_compare_memory(current_node->value, before_value, current_linked_list->element_byte_size)) {
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
if (new_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
@ -232,20 +225,21 @@ FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *curre
return FLEDASTY_ERROR_NONE;
}
previous_node = current_node;
current_node = current_node->next;
}
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *current_linked_list, void *after_value, void *value) {
if (current_linked_list == NULL || after_value == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyLinkedListNode *current_node = current_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, after_value, current_linked_list->element_byte_size)) {
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
while (current_node != NULL) {
if (hallocy_compare_memory(current_node->value, after_value, current_linked_list->element_byte_size)) {
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
if (new_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
@ -272,6 +266,12 @@ FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *curren
return FLEDASTY_ERROR_NONE;
}
current_node = current_node->next;
}
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_linked_list, const size_t index) {
if (current_linked_list == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
@ -317,15 +317,8 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link
FledastyLinkedListNode *previous_node = NULL;
FledastyLinkedListNode *current_node = current_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, value, current_linked_list->element_byte_size)) {
previous_node = current_node;
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
while (current_node != NULL) {
if (hallocy_compare_memory(current_node->value, value, current_linked_list->element_byte_size)) {
if (current_node == current_linked_list->end) {
current_linked_list->end = previous_node;
}
@ -348,6 +341,13 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link
return FLEDASTY_ERROR_NONE;
}
previous_node = current_node;
current_node = current_node->next;
}
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list) {
if (current_linked_list == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
@ -368,10 +368,10 @@ FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list
}
}
current_linked_list->size = 0;
current_linked_list->start = NULL;
current_linked_list->end = NULL;
current_linked_list->size = 0;
return FLEDASTY_ERROR_NONE;
}