feat(linked list): implemented before value function
This commit is contained in:
parent
8370ada3c4
commit
2f5f629def
3 changed files with 37 additions and 0 deletions
|
|
@ -45,6 +45,7 @@ FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_li
|
|||
FledastyError fledasty_linked_list_append(FledastyLinkedList *current_linked_list, void *value);
|
||||
|
||||
FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_linked_list, const size_t index, void *value);
|
||||
FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *current_linked_list, void *before_value, void *value);
|
||||
|
||||
static inline bool fledasty_linked_list_is_empty(FledastyLinkedList *current_linked_list) { return current_linked_list->size == 0; }
|
||||
|
||||
|
|
|
|||
|
|
@ -139,3 +139,36 @@ FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_l
|
|||
current_linked_list->size += 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *current_linked_list, void *before_value, void *value) {
|
||||
if (current_linked_list == NULL || before_value == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
FledastyLinkedListNode *previous_node = NULL;
|
||||
FledastyLinkedListNode *current_node = current_linked_list->start;
|
||||
while (current_node != NULL && !hallocy_compare_memory(current_node->value, before_value, current_linked_list->element_byte_size)) {
|
||||
previous_node = current_node;
|
||||
current_node = current_node->next;
|
||||
}
|
||||
|
||||
if (current_node == NULL) {
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
|
||||
|
||||
new_node->value = hallocy_malloc(current_linked_list->element_byte_size);
|
||||
hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size);
|
||||
|
||||
if (previous_node == NULL) {
|
||||
new_node->next = current_linked_list->start;
|
||||
current_linked_list->start = new_node;
|
||||
} else {
|
||||
new_node->next = previous_node->next;
|
||||
previous_node->next = new_node;
|
||||
}
|
||||
|
||||
current_linked_list->size += 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -112,6 +112,9 @@ int main() {
|
|||
|
||||
insert_value = 35;
|
||||
fledasty_linked_list_insert_at_index(&test_linked_list, 1, &insert_value);
|
||||
insert_value = 28;
|
||||
insert_at_value = 35;
|
||||
fledasty_linked_list_insert_before_value(&test_linked_list, &insert_at_value, &insert_value);
|
||||
|
||||
FledastyLinkedListNode *test_linked_list_node = test_linked_list.start;
|
||||
for (int i = 0; i < test_linked_list.size; i += 1) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue