Question: Hi , I need help with this project, code needs to be added where asked and malloc not calloc needs to be used. Code and
Hi , I need help with this project, code needs to be added where asked and malloc not calloc needs to be used. Code and instruction are provided below. Thanks.
The program dogs.c maintains records for canine patients at an animal hospital. Each dogs record has a name, a breed, a patient number, and owners last name. Complete the program so it uses a dynamically allocated linked list to store the records and contains the following functions:
append: ask the user to enter patient number, dogs name, dogs breed, and owners last name, then add the player to the end of the linked list.
It should check whether the dog has already existed by patient number. If so, the function should print a message and exit.
If the dog does not exist, allocate memory for the dog, store the data, and append the dog to the end of the linked list.
If the list is empty, the function should return the pointer to the newly created dog.
Otherwise, add the dog to the end of the linked list and return the pointer to the linked
list.
search: find the dog by name, print all the dogs information that matches the name. If the dog is not found, print a message.
print: print the name and number of all the dogs.
clear: when the user exists the program, all the memory allocated for the linked list should be
deallocated.
Note: use read_line function included in the program for reading in dog names, breeds, and owner last names.
Function process the linked list by using the malloc and free function properly.
#include#include #include #include #define NAME_LEN 30 struct dog{ int number; char dog_name[NAME_LEN+1]; char owner_last_name[NAME_LEN+1]; char breed[NAME_LEN+1]; struct dog *next; }; struct dog *append(struct dog *list); void search(struct dog *list); void print(struct dog *list); void clear(struct dog *list); int read_line(char str[], int n); /********************************************************** * main: Prompts the user to enter an operation code, * * then calls a function to perform the requested * * action. Repeats until the user enters the * * command 'q'. Prints an error message if the user * * enters an illegal code. * **********************************************************/ int main(void) { char code; struct dog *dog_list = NULL; printf("Operation Code: a for appending to the list, s for finding a dog" ", p for printing the list; q for quit. "); for (;;) { printf("Enter operation code: "); scanf(" %c", &code); while (getchar() != ' ') /* skips to end of line */ ; switch (code) { case 'a': dog_list = append(dog_list); break; case 's': search(dog_list); break; case 'p': print(dog_list); break; case 'q': clear(dog_list); return 0; default: printf("Illegal code "); } printf(" "); } } struct dog *append(struct dog *list){ //add your code here and remove the return NULL; statement return NULL; } void search (struct dog *list) { //add your code here } void print(struct dog *list){ //add your code here } void clear(struct dog *list) { //add your code here } int read_line(char str[], int n) { int ch, i = 0; while (isspace(ch = getchar())) ; str[i++] = ch; while ((ch = getchar()) != ' ') { if (i < n) str[i++] = ch; } str[i] = '\0'; return i; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
