feat(doubly linked list): implemented insert after value function

This commit is contained in:
Mineplay 2025-04-30 06:43:58 -05:00
parent e56ed3331f
commit 620ce5668c
3 changed files with 40 additions and 0 deletions

View file

@ -46,5 +46,6 @@ FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *curre
FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index, void *value);
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);
#endif

View file

@ -202,3 +202,40 @@ FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLink
current_doubly_linked_list->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *after_value, void *value) {
if (current_doubly_linked_list == NULL || after_value == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, after_value, current_doubly_linked_list->element_byte_size)) {
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
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 (current_node == current_doubly_linked_list->end) {
new_node->next = NULL;
new_node->previous = current_doubly_linked_list->end;
current_doubly_linked_list->end->next = new_node;
current_doubly_linked_list->end = new_node;
} else {
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;
}

View file

@ -151,6 +151,8 @@ int main() {
insert_value = 28;
insert_at_value = 35;
fledasty_doubly_linked_list_insert_before_value(&test_doubly_linked_list, &insert_at_value, &insert_value);
insert_value = 90;
fledasty_doubly_linked_list_insert_after_value(&test_doubly_linked_list, &insert_at_value, &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) {