The program dogs.c maintains records for canine patients at an animal hospital.
ID: 3685913 • 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() != ' ');
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
sample code-
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct dog{
char name[100];
char patientId[10];
struct dog *next;
};
struct dog *first=NULL,*last=NULL,*k;
void create(int n)
{
int i;
first=(struct dog*)malloc(sizeof(struct dog));
printf(" Enter name of the dog:");
scanf("%s",first->name);
printf(" Enter patientID of the dog:");
scanf("%s",first->patientId);
first->next=NULL;
last=first;
for(i=1;i {
k=(struct dog*)malloc(sizeof(struct dog));
printf(" Enter name of the dog:");
scanf("%s",k->name);
printf(" Enter patientId of the dog:");
scanf("%s",k->patientId);
k->next=NULL;
last->next=k;
last=k;
}
}
void display()
{
struct dog *t;
t=first;
while(t!=NULL)
{
printf(" The patientID of the dog:%s",t->patientID);
printf(" First name of the dog:%s",t->name);
t=t->next;
}
}
void insert()
{
char r[10];
int flag=0;
printf(" Enter the patientID u wanna insert:");
scanf("%s",r);
struct dog *t;
t=first;
while(t!=NULL)
{
if(strcmpi(r,t->patientID)==0)
{
k=(struct dog*)malloc(sizeof(struct dog));
printf(" Enter the first name of the dog:");
scanf("%s",k->name);
printf(" Enter the patientID of the dog:");
scanf("%s",k->patientID);
k->next=t->next;
t->next=k;
flag=1;
break;
}
t=t->next;
}
if(flag==0)
printf(" The element not found!!!");
}
void del()
{
struct dog *back,*t,*k;
char r[10];
int flag=0;
printf(" Enter the patientID u wanna delete:");
scanf("%s",r);
if(strcmpi(r,first->patientID)==0)
{
first=first->next;
flag=1;
}
else
{
back=first;
k=first->next;
while(k!=NULL)
{
if(strcmpi(r,k->patientID)==0)
{
back->next=k->next;
flag=1;
break;
}
}
}
if(flag==0)
printf(" The element not found!!!");
}
void search()
{
char r[10];
int flag=0;
printf(" Enter the patientID u wanna search:");
scanf("%s",r);
struct dog *t;
t=first;
while(t!=NULL)
{
if(strcmpi(r,t->patientID)==0)
{
printf(" The patientID found in the list!!! Dog name is %s",t->name);
flag=1;
break;
}t=t->next;
}
if(flag==0)
printf(" The patientID not in database!!");
}
int main()
{
int n,o;
while(o!=0)
{
printf(" MENU ");
printf(" Enter 1 for creating database");
printf(" Enter 2 for displaying database");
printf(" Enter 3 for inserting an record");
printf(" Enter 4 for deleting a record");
printf(" Enter 5 for searching a record");
printf(" Enter 0 for exit!");
printf(" Enter the choice:");
scanf("%d",&o);
switch(o)
{
case 1:printf(" Enter the maximum size of the database:");
scanf("%d",&n);
create(n);
break;
case 2:display();break;
case 3:insert();break;
case 4:del();break;
case 5:search();break;
case 0:exit(0);break;
default:printf(" You have entered a wrong choice!!!");
}
}
getch();
}
note-by using the above code with required modifications the given question can be answered.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.