Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include #include #define MAX_SPECIES_NAME_LENGTH 4096 // a struct to represent the date // a whale pod sighting was made struct date { int year;
#includeThe main function in species_delete_list.c has this code: char speciesargv[ 3i struct pod *firstPod = readsightingsFile(argv [1]); firstPod speciesDelete(species, firstPod); writeSightingsFile (argv[2], firstPod); Your task in this exercise is to complete this function: // delete the first occurrence of the given species from the list 1/ You should free the memory associated with the node you delete // and return the start of the list struct pod speciesDelete (char species[, struct pod *firstPod) // PUT YOUR CODE HERE Do not change any other function You are not permitted to use arrays in this exercise. You are not permitted to use malloc in this exercise, apart from its use in the supplied code. When you have completed the function speciesDelete this is how species_delete_list.c should behave: cat undeleted whales.txt 18/01/18 9 Pygmy right whale 01/09/17 21 Southern right whale 18/01/18 1 Pygmy right whale 18/01/18 1 Pyqmy right whale 16/02/18 4 Striped dolphin 16/02/18 4 Striped dolphin 18/01/18 1 Pygmy right whale 02/07/17 4 Common dolphin 16/02/18 5 Striped dolphin 03/02/18 6 Pyqmy right whale 18/01/18 3 Pygmy right whale ./merge_day_whales undeleted whales.txt deleted_whales.txt "Striped dolphi I1 cat deleted_whales.txt 18/01/18 9 Pygmy right whale 01/09/17 21 Southern right whale 18/01/18 1 Pygmy right whale 18/01/18 1 Pygmy right whale 16/02/18 4 Striped dolphin 18/01/18 1 Pygmy right whale 02/07/17 4 Common dolphin 16/02/18 5 Striped dolphin 03/02/18 6 Pygmy right whale 18/01/18 3 Pygmy right whale#include #include #define MAX_SPECIES_NAME_LENGTH 4096 // a struct to represent the date // a whale pod sighting was made struct date { int year; int month; int day; }; // a struct to represent a sighting // of a pod (group) of whales struct pod { struct pod *next; struct date *when; int howMany; char *species; }; struct pod *readSightingsFile(char filename[]); struct pod *readSighting(FILE *f); struct date *readDate(FILE *f); void writeSightingsFile(char filename[], struct pod *firstPod); void writeSighting(FILE *f, struct pod *p); void writeDate(FILE *f, struct date *d); void freeSightings(struct pod *p); struct pod * speciesDelete(char species[], struct pod *firstPod); int main(int argc, char *argv[]) { if (argc != 4) { fprintf(stderr, "Usage: %s ", argv[0]); return EXIT_FAILURE; } char * species = argv[3]; struct pod *firstPod = readSightingsFile(argv[1]); firstPod = speciesDelete(species, firstPod); writeSightingsFile(argv[2], firstPod); freeSightings(firstPod); return EXIT_SUCCESS; } // delete the first occurrence of the given species from the list // You should free the memory associated with the node you delete // and return the start of the list struct pod * speciesDelete(char species[], struct pod *firstPod) { // PUT YOUR CODE HERE } // // DO NOT CHANGE THE FUNCTIONS BELOW HERE // // return linked list of sightings read from filename // exit called if there is an error struct pod *readSightingsFile(char filename[]) { FILE *f = fopen(filename, "r"); if (f == NULL) { fprintf(stderr,"error: file '%s' can not open ", filename); exit(EXIT_FAILURE); } struct pod *firstSighting = NULL; struct pod *lastSighting = NULL; struct pod *sighting = readSighting(f); while (sighting != NULL) { if (firstSighting == NULL) { firstSighting = sighting; firstSighting->next = NULL; } else { lastSighting->next = sighting; } lastSighting = sighting; sighting = readSighting(f); } return firstSighting; } // read a whale sighting (date, number of whales, whale species) // return a pointer to a malloced struct containing these details // return NULL if a sighting can not be read struct pod *readSighting(FILE *f) { struct pod *p = malloc(sizeof (struct pod)); if (p == NULL) { fprintf(stderr, "out of memory "); exit(EXIT_FAILURE); } p->next = NULL; p->when = readDate(f); if (p->when == NULL) { free(p); return NULL; } int nScanned = fscanf(f, "%d", &(p->howMany)); if (nScanned != 1) { free(p); return NULL; } fgetc(f); char speciesBuffer[MAX_SPECIES_NAME_LENGTH]; if (fgets(speciesBuffer, MAX_SPECIES_NAME_LENGTH, f) == NULL) { free(p); return NULL; } // finish string at ' ' if there is one char *newlinePtr = strchr(speciesBuffer, ' '); if (newlinePtr != NULL) { *newlinePtr = '\0'; } // also finish string at ' ' if there is one - files from Windows will newlinePtr = strchr(speciesBuffer, ' '); if (newlinePtr != NULL) { *newlinePtr = '\0'; } // malloc a char array long enough to hold species name // and copy species to it p->species = malloc(strlen(speciesBuffer) + 1); if (p->species == NULL) { fprintf(stderr, "out of memory "); exit(EXIT_FAILURE); } strcpy(p->species, speciesBuffer); return p; } // read a date in day/month/year format from stream f // return a pointer to a malloced date struct containing them // return NULL if a date can not be read struct date *readDate(FILE *f) { struct date *d = malloc(sizeof (struct date)); if (d == NULL) { fprintf(stderr, "out of memory "); exit(1); } int n_scanned = fscanf(f, "%d/%d/%d", &(d->day), &(d->month), &(d->year)); if (n_scanned != 3) { free(d); return NULL; } return d; } // print linked list of sightings to filename void writeSightingsFile(char filename[], struct pod *firstPod) { FILE *f = fopen(filename, "w"); if (f == NULL) { fprintf(stderr,"error: file '%s' can not open ", filename); exit(EXIT_FAILURE); } struct pod *p = firstPod; while (p != NULL) { writeSighting(f, p); p = p->next; } } // print pod details to stream f void writeSighting(FILE *f, struct pod *p) { writeDate(f, p->when); fprintf(f, " %2d %s ", p->howMany, p->species); } // print date to stream f void writeDate(FILE *f, struct date *d) { fprintf(f, "%02d/%02d/%02d", d->day, d->month, d->year); } // free the list of sightings void freeSightings(struct pod *p) { struct pod *curr = p; while (curr != NULL) { struct pod *next = curr->next; free(curr->species); free(curr->when); free(curr); curr = next; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started