feat(doubly linked list): implemented insert at index
This commit is contained in:
parent
d0c552362b
commit
b969e85412
4 changed files with 63 additions and 0 deletions
|
|
@ -44,4 +44,6 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_dou
|
|||
|
||||
FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *current_doubly_linked_list, void *value);
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index, void *value);
|
||||
|
||||
#endif
|
||||
|
|
@ -113,3 +113,54 @@ FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *curre
|
|||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index, void *value) {
|
||||
if (current_doubly_linked_list == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (index > current_doubly_linked_list->size) {
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
|
||||
|
||||
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
|
||||
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
|
||||
|
||||
if (index == 0) {
|
||||
new_node->previous = NULL;
|
||||
new_node->next = current_doubly_linked_list->start;
|
||||
|
||||
current_doubly_linked_list->start->previous = new_node;
|
||||
current_doubly_linked_list->start = new_node;
|
||||
} else if (index == current_doubly_linked_list->size) {
|
||||
new_node->previous = current_doubly_linked_list->end;
|
||||
new_node->next = NULL;
|
||||
|
||||
current_doubly_linked_list->end->next = new_node;
|
||||
current_doubly_linked_list->end = new_node;
|
||||
} else {
|
||||
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 - 1; 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 - 1; node -= 1) {
|
||||
current_node = current_node->previous;
|
||||
}
|
||||
}
|
||||
|
||||
new_node->previous = current_node;
|
||||
new_node->next = current_node->next;
|
||||
|
||||
current_node->next->previous = new_node;
|
||||
current_node->next = new_node;
|
||||
}
|
||||
|
||||
current_doubly_linked_list->size += 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,6 +123,7 @@ FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_l
|
|||
new_node->next = current_linked_list->start;
|
||||
current_linked_list->start = new_node;
|
||||
} else if (index == current_linked_list->size) {
|
||||
new_node->next = NULL;
|
||||
current_linked_list->end->next = new_node;
|
||||
current_linked_list->end = new_node;
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -146,12 +146,21 @@ int main() {
|
|||
fledasty_doubly_linked_list_append(&test_doubly_linked_list, &i);
|
||||
}
|
||||
|
||||
insert_value = 35;
|
||||
fledasty_doubly_linked_list_insert_at_index(&test_doubly_linked_list, test_doubly_linked_list.size - 1, &insert_value);
|
||||
|
||||
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);
|
||||
test_doubly_linked_list_node = test_doubly_linked_list_node->next;
|
||||
}
|
||||
|
||||
test_doubly_linked_list_node = test_doubly_linked_list.end;
|
||||
for (int i = 0; i < test_doubly_linked_list.size; i += 1) {
|
||||
printf("Doubly linked list get backwards: %d\n", *(int*)test_doubly_linked_list_node->value);
|
||||
test_doubly_linked_list_node = test_doubly_linked_list_node->previous;
|
||||
}
|
||||
|
||||
fledasty_doubly_list_destroy(&test_doubly_linked_list);
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue