feat(doubly linked list): implemented remove at index function
This commit is contained in:
parent
bd6b4b1335
commit
e2b88338a3
3 changed files with 49 additions and 1 deletions
|
|
@ -48,6 +48,8 @@ FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedLi
|
|||
FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *before_value, void *value);
|
||||
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);
|
||||
|
||||
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; }
|
||||
|
||||
|
|
|
|||
|
|
@ -240,6 +240,50 @@ FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinke
|
|||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index) {
|
||||
if (current_doubly_linked_list == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (index >= current_doubly_linked_list->size) {
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *current_node = NULL;
|
||||
if (index < current_doubly_linked_list->size / 2) {
|
||||
current_node = current_doubly_linked_list->start;
|
||||
for (size_t node = 0; node < index; node += 1) {
|
||||
current_node = current_node->next;
|
||||
}
|
||||
} else {
|
||||
current_node = current_doubly_linked_list->end;
|
||||
for (size_t node = current_doubly_linked_list->size - 1; node > index; node -= 1) {
|
||||
current_node = current_node->previous;
|
||||
}
|
||||
}
|
||||
|
||||
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->value);
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -154,6 +154,8 @@ int main() {
|
|||
insert_value = 90;
|
||||
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);
|
||||
|
||||
FledastyDoublyLinkedListNode *test_doubly_linked_list_node = test_doubly_linked_list.start;
|
||||
for (int i = 0; i < test_doubly_linked_list.size; i += 1) {
|
||||
printf("Doubly linked list get: %d\n", *(int*)test_doubly_linked_list_node->value);
|
||||
|
|
@ -166,7 +168,7 @@ int main() {
|
|||
test_doubly_linked_list_node = test_doubly_linked_list_node->previous;
|
||||
}
|
||||
|
||||
if (fledasty_linked_list_has_value(&test_linked_list, &insert_value)) {
|
||||
if (fledasty_doubly_linked_list_has_value(&test_doubly_linked_list, &insert_value)) {
|
||||
printf("Doubly linked list contains %d\n", insert_value);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue