diff --git a/src/list.c b/src/list.c index 6c2c186..36f9eba 100644 --- a/src/list.c +++ b/src/list.c @@ -98,74 +98,15 @@ int List_remove(List *list, int element) { return ERRNO_005; } - for (int i = positionToDelete; i < list->_currentSize + 1; i++) { + int elementToDelete = list->_array[positionToDelete]; + for (int i = positionToDelete; i < list->_currentSize; i++) { list->_array[i] = list->_array[i + 1]; } list->_currentSize--; - return 0; -} - -int List_merge(List *list, int start_index, int mid_index, int end_index, bool reverse) { - List *left = List_slice(list, start_index, mid_index + 1); - if (!left) { - return 1; - } - List *right = List_slice(list, mid_index + 1, end_index + 1); - if (!right) { - List_destroy(&left); - return 1; - } - - int left_index = 0; - int right_index = 0; - int left_length = List_length(left); - int right_length = List_length(right); - - for (int i = start_index; i < end_index + 1; i++) { - if (left_index == left_length) { - list->_array[i] = right->_array[right_index++]; - } else if (right_index == right_length) { - list->_array[i] = left->_array[left_index++]; - } else if (reverse) { - if (left->_array[left_index] > right->_array[right_index]) { - list->_array[i] = left->_array[left_index++]; - } else { - list->_array[i] = right->_array[right_index++]; - } - } else { - if (left->_array[left_index] < right->_array[right_index]) { - list->_array[i] = left->_array[left_index++]; - } else { - list->_array[i] = right->_array[right_index++]; - } - } - } - - List_destroy(&left); - List_destroy(&right); - - return 0; -} - -int List_mergeSort(List *list, int start_index, int end_index, bool reverse) { - if (start_index < end_index) { - int mid_index = (start_index + end_index) / 2; - if (List_mergeSort(list, start_index, mid_index, reverse) != 0) { - return 1; - } - if (List_mergeSort(list, mid_index + 1, end_index, reverse) != 0) { - return 1; - } - if (List_merge(list, start_index, mid_index, end_index, reverse) != 0) { - return 1; - } - } - - return 0; + return elementToDelete; } - int List_sort(List *list, bool reverse) { return List_mergeSort(list, 0, list->_currentSize, reverse); } @@ -301,4 +242,63 @@ List *List_createList(int maxSize, int currentSize) { list->_currentSize = currentSize; return list; +} + +int List_merge(List *list, int start_index, int mid_index, int end_index, bool reverse) { + List *left = List_slice(list, start_index, mid_index + 1); + if (!left) { + return 1; + } + List *right = List_slice(list, mid_index + 1, end_index + 1); + if (!right) { + List_destroy(&left); + return 1; + } + + int left_index = 0; + int right_index = 0; + int left_length = List_length(left); + int right_length = List_length(right); + + for (int i = start_index; i < end_index + 1; i++) { + if (left_index == left_length) { + list->_array[i] = right->_array[right_index++]; + } else if (right_index == right_length) { + list->_array[i] = left->_array[left_index++]; + } else if (reverse) { + if (left->_array[left_index] > right->_array[right_index]) { + list->_array[i] = left->_array[left_index++]; + } else { + list->_array[i] = right->_array[right_index++]; + } + } else { + if (left->_array[left_index] < right->_array[right_index]) { + list->_array[i] = left->_array[left_index++]; + } else { + list->_array[i] = right->_array[right_index++]; + } + } + } + + List_destroy(&left); + List_destroy(&right); + + return 0; +} + +int List_mergeSort(List *list, int start_index, int end_index, bool reverse) { + if (start_index < end_index) { + int mid_index = (start_index + end_index) / 2; + if (List_mergeSort(list, start_index, mid_index, reverse) != 0) { + return 1; + } + if (List_mergeSort(list, mid_index + 1, end_index, reverse) != 0) { + return 1; + } + if (List_merge(list, start_index, mid_index, end_index, reverse) != 0) { + return 1; + } + } + + return 0; } \ No newline at end of file diff --git a/src/list.h b/src/list.h index 3886e25..1d7bfff 100644 --- a/src/list.h +++ b/src/list.h @@ -107,7 +107,7 @@ int List_insert(List *list, int index, int element); * * Returns * ---------------------------- - * int (0 for success, Non-0 for error) + * int (The deleted element) */ int List_remove(List *list, int element); diff --git a/src/test/test.c b/src/test/test.c index 214ce3b..60bb1d9 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -147,9 +147,10 @@ void shouldRemoveElementFromList() { List_append_all(list, 4, 0, 1, 2, 0); // Act - List_remove(list, 0); + int deletedElement = List_remove(list, 0); // Assert + assert(deletedElement == 0); assert(List_length(list) == 3); assert(List_get(list, 0) == 1); assert(List_get(list, 1) == 2);