feat(linked list): implemented remove value
This commit is contained in:
parent
e29d271dc4
commit
c7b866ce1d
3 changed files with 56 additions and 22 deletions
|
|
@ -48,7 +48,8 @@ FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_l
|
|||
FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *current_linked_list, void *before_value, void *value);
|
||||
FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *current_linked_list, void *after_value, void *value);
|
||||
|
||||
FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_dynamic_array, const size_t index);
|
||||
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);
|
||||
|
||||
bool fledasty_linked_list_has_value(FledastyLinkedList *current_linked_list, void *value);
|
||||
static inline bool fledasty_linked_list_is_empty(FledastyLinkedList *current_linked_list) { return current_linked_list->size == 0; }
|
||||
|
|
|
|||
|
|
@ -215,29 +215,60 @@ FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_l
|
|||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
if (index == 0) {
|
||||
FledastyLinkedListNode *remove_node = current_linked_list->start;
|
||||
current_linked_list->start = current_linked_list->start->next;
|
||||
|
||||
hallocy_free(remove_node->value);
|
||||
hallocy_free(remove_node);
|
||||
} else {
|
||||
FledastyLinkedListNode *current_node = current_linked_list->start;
|
||||
for (size_t node = 0; node < index - 1; node += 1) {
|
||||
current_node = current_node->next;
|
||||
}
|
||||
|
||||
FledastyLinkedListNode *remove_node = current_node->next;
|
||||
current_node->next = remove_node->next;
|
||||
|
||||
hallocy_free(remove_node->value);
|
||||
hallocy_free(remove_node);
|
||||
|
||||
if (index == current_linked_list->size - 1) {
|
||||
current_linked_list->end = current_node;
|
||||
}
|
||||
FledastyLinkedListNode *previous_node = NULL;
|
||||
FledastyLinkedListNode *current_node = current_linked_list->start;
|
||||
for (size_t node = 0; node < index; node += 1) {
|
||||
previous_node = current_node;
|
||||
current_node = current_node->next;
|
||||
}
|
||||
|
||||
if (current_node->next == NULL) {
|
||||
current_linked_list->end = previous_node;
|
||||
}
|
||||
|
||||
if (previous_node == NULL) {
|
||||
current_linked_list->start = current_node->next;
|
||||
} else {
|
||||
previous_node->next = 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_remove_value(FledastyLinkedList *current_linked_list, void *value) {
|
||||
if (current_linked_list == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (current_node->next == NULL) {
|
||||
current_linked_list->end = previous_node;
|
||||
}
|
||||
|
||||
if (previous_node == NULL) {
|
||||
current_linked_list->start = current_node->next;
|
||||
} else {
|
||||
previous_node->next = current_node->next;
|
||||
}
|
||||
|
||||
hallocy_free(current_node->next);
|
||||
hallocy_free(current_node);
|
||||
|
||||
|
||||
current_linked_list->size -= 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -119,6 +119,8 @@ int main() {
|
|||
fledasty_linked_list_insert_after_value(&test_linked_list, &insert_at_value, &insert_value);
|
||||
|
||||
fledasty_linked_list_remove_at_index(&test_linked_list, 2);
|
||||
remove_value = 0;
|
||||
fledasty_linked_list_remove_value(&test_linked_list, &remove_value);
|
||||
|
||||
FledastyLinkedListNode *test_linked_list_node = test_linked_list.start;
|
||||
for (int i = 0; i < test_linked_list.size; i += 1) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue