COP 3223H meeting -*- Outline -*- * Examples of Singly-Linked lists ** client code *** list_equal ------------------------------------------ CLIENT CODE: LIST_EQUAL Determine if two lists have the same elements in the same order // ensures: result is true just when // each element of lst1 is equal (==) // to the corresponding element of lst2. extern bool list_equal(list lst1, list lst2) { ------------------------------------------ It's possible to write code that uses details of the list structure: ... return (lst1 == NULL && lst2 == NULL) || (lst1 != NULL && lst2 != NULL && lst1->value == lst2->value && list_equal(lst1->next, lst2->next)); } But better to write code that is more abstract: ... return (list_isEmpty(lst1) && list_isEmpty(lst2)) || (!list_isEmpty(lst1) && !list_isEmpty(lst2) && list_first(lst1) == list_first(lst2) && list_equal(list_tail(lst1), list_tail(lst2))); } Q: Can we write list_equal without using recursion? ... while (!list_isEmpty(lst1) && !list_isEmpty(lst2)) { if (list_first(lst1) != list_first(lst2)) { return false; } lst1 = list_tail(lst1); lst2 = list_tail(lst2); } return list_isEmpty(lst1) == list_isEmpty(lst2); } ... // or a more explicit version while (lst1 != NULL && lst2 != NULL) { if (lst1->value != lst2->value) return false; } lst1 = lst1->next; lst2 = lst2->next; } return lst1 == lst2; // true if both NULL } *** list_length ------------------------------------------ FOR YOU TO DO Implement the following function // ensures: result is the number of elements in lst extern int list_length(list lst); (a) using recursion (b) using a while loop ------------------------------------------ // (a) recursive version: int list_length(list lst) { if (list_isEmpty(lst)) { return 0; } else { return 1+list_length(list_tail(lst)); } } // (b) iterative version: int list_length(list lst) { int len = 0; while (lst != NULL) { len++; lst = lst->next; } return len; } *** list_sum ------------------------------------------ FOR YOU TO DO If ELEM is a numeric type we can write // ensures: result is sum of the elements in lst extern ELEM list_sum(list lst); (a) using a while loop (b) using a recursion ------------------------------------------ // (a) recursive code: ELEM list_sum(list lst) { if (list_isEmpty(lst)) { return 0; } else { return list_first(lst) + list_sum(list_tail(lst)); } } // (b) iterative code: ELEM list_sum(list lst) { ELEM sum = 0; while (lst != NULL) { sum += lst->value; lst = lst->next; } return sum; } *** list_copy ------------------------------------------ FOR YOU TO DO // ensures: result is copy of list, // which is equal to lst // but does not share storage with lst extern list list_copy(list lst); (a) using a while loop (b) using a recursion ------------------------------------------ // (a) recursive code: list list_copy(list lst) { if (list_isEmpty(lst)) { return NIL; } else { return list_cons(list_first(lst), list_copy(list_tail(lst))); } } // (b) iterative code: list list_copy(list lst) { if (lst == NULL) { return NIL; } list ret = list_cons(list_first(lst), NIL); // p points to where we are inserting (the last node) list p = ret; lst = lst->next; while (lst != NULL) { p->next = list_cons(list_first(lst), NIL); p = p->next; lst = lst->next; } return ret; }