|
What should be the state of the inner iterator after each function? |
1. After creation of a new list (with listCreate function) the list is empty so obviously the iterator should point to NULL. 2. When copying a list (with listCopy) the iterator of the new list should point to the same clone node that the original iterator is pointing to in the original list. 3. After creating a new list with listFilter function, we get a new list that contains elements, therefore we expect the iterator to point to the first element in the filtered list. The state of the original list is not defined after this operation. 4. listGetFirst() - sets the iterator to point to the first element in the list. 5. listGetNext() - sets the iterator to point to the next element from the the one currently pointed. If the end of the list was reached, the iterator should be set to NULL. 6. listInsertLast(), listInsertFirst(), listInsertAfterCurrent(), listInsertBeforeCurrent() - The iterator state should not be changed. 7. listRemoveCurrent() - the iterator should point to NULL at the end. 8. listSort() - the iterator will still point to the same node in order: if the list before sorting was: 1, 5, 2, 6. and the iterator pointed to "5". after the sorting: 1, 2, 5, 6. the iterator should still points to the second element in the list and therefore will point to "2". 9. listClear() - the iterator should point to NULL, as there are no elements to point to. 10. listGetCurrent(), listGetSize() - the iterator should not change.
|
When iterating over the list using listGetFirst and listGetNext, do we return a copy to the element or the element itself? |
You should return the element itself.
|
Do we need to implement the listSort with or without a key element? |
You must implement the function as it is defined in the header file interface:
ListResult listSort(List list, CompareListElements compareElement);
Please ignore the ListSortKey parameter that is mentioned in the comments.
|
What will happen if a NULL element is given to the listInsert functions? |
You can assume that a NULL element will not be inserted, and therefore you don't need to check this case.
|
What happens if we send key = NULL to the listFilter function? |
You can assume that NULL will not be sent as key to the listFilter function, and therefore you don't need to check this case.
|
In the listSort function, in a case that for two elements a,b compare(a,b) = 0, what should be the order between the elements? |
If the compare function decides that the two elements are equal (return 0) then we don't care what will be the order between the two elements: Consider a case we have a list of strings, and the compare function key is which string is longer, and lets say the two only elements in the list are "aaa" "bbb" , both have length 3, and compareLength("aaa", "bbb") =0. then this is how a typical test will look like:
listSort(listStrings, compareLength); ASSERT(compareLength("aaa",listGetFirst(listStrings)) == 0); //dont care if "aaa" or "bbb" ASSERT(compareLength("bbb",listGetNext(listStrings)) == 0); //dont care if "aaa" or "bbb" ASSERT(compareLength("Hello World",listGetNext(listStrings)) == 0); ASSERT(listGetNext(listStrings) == NULL);
|
What order should the list sort? |
The compare function behavior should be: return a value greater than 0 if the first element is greater, 0 if they're equal and a value smaller than 0 if the second element is greater.
(Notice: this is opposite then the behavior described in the list.h file)
Here is a code example:
static ListElement copyString(ListElement str) { assert(str); char* copy = malloc(strlen(str)+1); return copy != NULL ? strcpy(copy, str) : NULL; }
static void freeString(ListElement str){ free(str); }
static int compareString(ListElement str1, ListElement str2){ return strcmp(str1, str2); }
boo testListSort{ char* a[5] = {"aaa","NI","hello mister fish","I", "bbb"}; List list1 = listCreate(copyString,freeString); for (int i=0;i <5; ++i){ listInsertFirst(list1,a[i]); } listSort(list1,compareString); ASSERT_TEST(strcmp(listGetFirst(list1),"I") == 0); ASSERT_TEST(strcmp(listGetNext(list1),"NI") == 0); ASSERT_TEST(strcmp(listGetNext(list1),"aaa") == 0); ASSERT_TEST(strcmp(listGetNext(list1),"bbb") == 0); ASSERT_TEST(strcmp(listGetNext(list1),"hello mister fish") == 0); listDestroy(list1); }
|
|