Return deleted element from List_remove()

main
Hammy 3 years ago
parent 68b8358118
commit 00819f6edc

@ -98,74 +98,15 @@ int List_remove(List *list, int element) {
return ERRNO_005; 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->_array[i] = list->_array[i + 1];
} }
list->_currentSize--; list->_currentSize--;
return 0; return elementToDelete;
}
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;
} }
int List_sort(List *list, bool reverse) { int List_sort(List *list, bool reverse) {
return List_mergeSort(list, 0, list->_currentSize, reverse); return List_mergeSort(list, 0, list->_currentSize, reverse);
} }
@ -302,3 +243,62 @@ List *List_createList(int maxSize, int currentSize) {
return list; 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;
}

@ -107,7 +107,7 @@ int List_insert(List *list, int index, int element);
* *
* Returns * Returns
* ---------------------------- * ----------------------------
* int (0 for success, Non-0 for error) * int (The deleted element)
*/ */
int List_remove(List *list, int element); int List_remove(List *list, int element);

@ -147,9 +147,10 @@ void shouldRemoveElementFromList() {
List_append_all(list, 4, 0, 1, 2, 0); List_append_all(list, 4, 0, 1, 2, 0);
// Act // Act
List_remove(list, 0); int deletedElement = List_remove(list, 0);
// Assert // Assert
assert(deletedElement == 0);
assert(List_length(list) == 3); assert(List_length(list) == 3);
assert(List_get(list, 0) == 1); assert(List_get(list, 0) == 1);
assert(List_get(list, 1) == 2); assert(List_get(list, 1) == 2);

Loading…
Cancel
Save