The program dogs.c maintains records for canine patients at an animal hospital.
ID: 3687856 • Letter: T
Question
The program dogs.c maintains records for canine patients at an animal hospital. Each dog’s record
has a name, a breed, a patient number, and owner’s last name. Complete the program so it uses a
dynamically allocated linked list to store the records and contains the following functions:
1. append: ask the user to enter patient number, dog’s name, dog’s breed, and owner’s last
name, then add the player to the end of the linked list.
a. It should check whether the dog has already existed by patient number. If so, the
function should print a message and exit.
b. 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.
c. If the list is empty, the function should return the pointer to the newly created dog.
d. Otherwise, add the dog to the end of the linked list and return the pointer to the linked
list.
2. search: find the dog by name, print all the dog’s information that matches the name. If the
dog is not found, print a message.
3. print: print the name and number of all the dogs.
4.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.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#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] = ''; return i; }
Explanation / Answer
struct dog {
char dog_name[20];
char owner_name[20];
char breed[20];
int patient_number;
struct dog *next;
};
struct dog *head = (struct dog *) NULL;
struct dog *end = (struct dog *) NULL;
void add( struct dog *ptr)
{
ptr = (struct dog *) calloc( 1, sizeof(struct dog ) );
if( ptr == NULL )
printf("Error");
else {
char dog_name[20];
char owner_name[20];
char breed[20];
int patient_number;
printf("Enter dog Name");
strcpy( ptr->dog_name, dog_name );
printf("Enter Owner Name");
strcpy( ptr->owner_name, owner_name );
printf("Enter Pateient Number");
ptr->patient_number = patient_number;
printf("Enter Breed Name");
strcpy( ptr->breed_name, breed_name );
if(checkDuplicate(ptr))
{
if( head == NULL )
head = ptr;
end->next = ptr;
ptr->next = NULL;
end = ptr;
}
else printf("Already exists");
}
}
void print( struct dog *ptr )
{
int count;
while( ptr != NULL )
{
printf("Dog Name ->%s ", ptr->dog_name );
printf("Dog Number ->%s ", ptr->patient_number );
ptr = ptr->next;
count++;
}
printf("Total Number of Dogs: %d", count);
}
void clear( struct dog *head)
{
while(head!=null)
{
struct dog *temp, *prev;
temp = ptr;
prev = head;
if( temp == prev ) {
head = head->next;
if( end == temp )
end = end->next;
free( temp );
}
else {
while( prev->next != temp ) {
prev = prev->next;
}
prev->next = temp->next;
if( end == temp )
end = prev;
free( temp );
}
}
}
struct dog * searchname( char *name )
{
while( strcmp( name, head->dog_name ) != 0 ) {
head = head->next;
if( head == NULL )
break;
}
return head;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.