Add List_count()

main
Hammy 3 years ago
parent 2e5a5c5b64
commit f23df82757

@ -219,6 +219,23 @@ int List_extend(List *list_to_extend, List *input_list) {
return 0;
}
int List_count(List *list, int element) {
if (!list) {
return 1;
}
int count = 0;
int listLength = List_length(list);
for (int i = 0; i < listLength; i++) {
if (element == List_get(list, i)) {
count++;
}
}
return count;
}
int List_length(List *list) {
return list->_currentSize + 1;
}

@ -241,6 +241,21 @@ void shouldPopElementFromList() {
printSuccess(__func__);
}
void shouldCountElementsInList() {
// Arrange
List *list = List_new();
List_append_all(list, 5, 0, 1, 1, 3, 4, 5);
// Act
int count = List_count(list, 1);
// Assert
assert(count == 2);
List_destroy(&list);
printSuccess(__func__);
}
int main() {
printf("============================================");
printf("\nSTART TESTING");
@ -258,6 +273,7 @@ int main() {
shouldExtendList();
shouldDeleteElementFromList();
shouldPopElementFromList();
shouldCountElementsInList();
printf("\n\n============================================");
printf("\nFINISH TESTING");
printf("\n============================================");

Loading…
Cancel
Save