feat(doubly linked list): implemented insert after value function
This commit is contained in:
parent
e56ed3331f
commit
620ce5668c
3 changed files with 40 additions and 0 deletions
|
|
@ -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_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_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
|
#endif
|
||||||
|
|
@ -202,3 +202,40 @@ FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLink
|
||||||
current_doubly_linked_list->size += 1;
|
current_doubly_linked_list->size += 1;
|
||||||
return FLEDASTY_ERROR_NONE;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -151,6 +151,8 @@ int main() {
|
||||||
insert_value = 28;
|
insert_value = 28;
|
||||||
insert_at_value = 35;
|
insert_at_value = 35;
|
||||||
fledasty_doubly_linked_list_insert_before_value(&test_doubly_linked_list, &insert_at_value, &insert_value);
|
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;
|
FledastyDoublyLinkedListNode *test_doubly_linked_list_node = test_doubly_linked_list.start;
|
||||||
for (int i = 0; i < test_doubly_linked_list.size; i += 1) {
|
for (int i = 0; i < test_doubly_linked_list.size; i += 1) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue