daily pastebin goal
74%
SHARE
TWEET

Untitled

a guest May 16th, 2018 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // merge sightings of the same species on the same day
  2. // into the first sighting of the  species on that day
  3. // The number of whales seen in subsequent sightings is added
  4. // to the number of whales seen in the first sighting.
  5. // The subsequent sightings are deleted from the list
  6. // and all associated memory freed.
  7.  
  8. // compare equality of string 'species'
  9. int same_species(struct pod *pod, struct pod *comp) {
  10.    return (strcmp(pod->species, comp->species) == 0);
  11. }
  12.  
  13. // check dates are the same
  14. int same_day(struct date *day, struct date *comp) {
  15.    return (day->year  == comp->year &&
  16.            day->month == comp->month &&
  17.            day->day   == comp->day);
  18. }
  19.  
  20. // check same day and same date
  21. int same_day_species(struct pod *pod, struct pod *comp) {
  22.    return (same_species(pod, comp) && same_day(pod->when, comp->when));
  23. }
  24.  
  25. // delete a pod strct
  26. void delete_pod(struct pod *curr) {
  27.    // don't delete if null
  28.    if (!curr) return;
  29.    free(curr->species);
  30.    free(curr->when);
  31.    free(curr);
  32. }
  33.  
  34. // helper for main
  35. struct pod *merge_helper(struct pod *comp, struct pod *curr) {
  36.    if (curr) {
  37.       struct pod *next = curr->next;
  38.       if (same_day_species(comp, curr)) {
  39.          comp->how_many += curr->how_many;
  40.          delete_pod(curr);
  41.          curr = merge_helper(comp, next);
  42.       } else {
  43.          curr->next = merge_helper(comp, next);
  44.       }
  45.    }
  46.    return curr;
  47. }
  48.  
  49. // main
  50. void merge_day_whales(struct pod *first_pod) {
  51.     struct pod *curr = first_pod;
  52.     while (curr->next) {
  53.       curr->next = merge_helper(curr, curr->next);
  54.       curr = curr->next;
  55.     }
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top