Add List_extend()

main
Hammy 3 years ago
parent 265998a649
commit 4db5bfc107

@ -69,6 +69,7 @@ int List_append(List *list, int element) {
} }
list->_array = temp; list->_array = temp;
} }
list->_array[++list->_currentSize] = element; list->_array[++list->_currentSize] = element;
return 0; return 0;
@ -251,6 +252,21 @@ List *List_slice(List *list, int start_index, int end_index) {
return slicedList; return slicedList;
} }
int List_extend(List *list_to_extend, List *input_list) {
if (!list_to_extend || !input_list) {
return 1;
}
for (int i = 0; i < input_list->_currentSize + 1; i++) {
int returnCode = List_append(list_to_extend, List_get(input_list, i));
if (returnCode != 0) {
return returnCode;
}
}
return 0;
}
int List_length(List *list) { int List_length(List *list) {
return list->_currentSize + 1; return list->_currentSize + 1;
} }

@ -198,6 +198,29 @@ int List_clear(List *list);
*/ */
List *List_slice(List *list, int start_index, int end_index); List *List_slice(List *list, int start_index, int end_index);
/*
*
* Description
* ----------------------------
* Extend the given list_to_extend with elements from given input_list
*
* Params
* ----------------------------
* *list_to_extend the list to extend with elements from input_list
* *input_list the list containing elements to insert into list_to_extend
*
* Example
* ----------------------------
* Given list1 -> [1, 2] AND list2 -> [3, 4, 5]
* When List_extend(list1, list2);
* Then list1 -> [1, 2, 3, 4, 5]
*
* Returns
* ----------------------------
* int (0 for success, Non-0 for error)
*/
int List_extend(List *list_to_extend, List *input_list);
/* /*
* *
* Description * Description

@ -179,6 +179,28 @@ void shouldReverseList() {
printSuccess(__func__); printSuccess(__func__);
} }
void shouldExtendList() {
// Arrange
List *list_to_extend = List_new();
List *input_list = List_new();
List_append_all(list_to_extend, 2, 1, 2);
List_append_all(input_list, 3, 3, 4, 5);
// Act
int returnCode = List_extend(list_to_extend, input_list);
// Assert
assert(returnCode == 0);
assert(List_length(list_to_extend) == 5);
for (int i = 0; i < 5; i++) {
assert(List_get(list_to_extend, i) == i + 1);
}
List_destroy(&list_to_extend);
List_destroy(&input_list);
printSuccess(__func__);
}
int main() { int main() {
printf("============================================"); printf("============================================");
printf("\nSTART TESTING"); printf("\nSTART TESTING");
@ -193,6 +215,7 @@ int main() {
shouldInsertElementIntoListIndexZero(); shouldInsertElementIntoListIndexZero();
shouldRemoveElementFromList(); shouldRemoveElementFromList();
shouldReverseList(); shouldReverseList();
shouldExtendList();
printf("\n\n============================================"); printf("\n\n============================================");
printf("\nFINISH TESTING"); printf("\nFINISH TESTING");
printf("\n============================================"); printf("\n============================================");

Loading…
Cancel
Save