feat(linked list): implemented remove at index function
This commit is contained in:
parent
ffa7bd27eb
commit
e29d271dc4
3 changed files with 40 additions and 0 deletions
|
|
@ -48,6 +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);
|
||||
|
||||
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; }
|
||||
|
||||
|
|
|
|||
|
|
@ -206,6 +206,42 @@ FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *curren
|
|||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (index >= current_linked_list->size) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
current_linked_list->size -= 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
bool fledasty_linked_list_has_value(FledastyLinkedList *current_linked_list, void *value) {
|
||||
if (current_linked_list == NULL || value == NULL) {
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -118,6 +118,8 @@ int main() {
|
|||
insert_value = 90;
|
||||
fledasty_linked_list_insert_after_value(&test_linked_list, &insert_at_value, &insert_value);
|
||||
|
||||
fledasty_linked_list_remove_at_index(&test_linked_list, 2);
|
||||
|
||||
FledastyLinkedListNode *test_linked_list_node = test_linked_list.start;
|
||||
for (int i = 0; i < test_linked_list.size; i += 1) {
|
||||
printf("Linked list get: %d\n", *(int*)test_linked_list_node->value);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue