Make sure the makefile compiles and runs. Executable code: Dogs.h #ifndef dog_h
ID: 3693231 • Letter: M
Question
Make sure the makefile compiles and runs.
Executable code:
Dogs.h
#ifndef dog_h
#define dog_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;
};
/*function prototypes*/
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);
#endif
Dogs.c:
#include
#include
#include
#include
#include"dogs.h"
struct dog *append(struct dog *list){
struct dog *cur, *new_node;
new_node = malloc(sizeof(struct dog));
if (new_node == NULL) {
printf("Database is full; can't add more dogs. ");
return list;
}
printf("Enter dog's patient number: ");
scanf("%d", &new_node->number);
for (cur = list;cur != NULL;cur = cur->next)
if (cur != NULL && new_node->number == cur->number) {
printf("Patient already exists. ");
free(new_node);
return list;
}
printf("Enter dog's name: ");
read_line(new_node->dog_name, NAME_LEN);
printf("Enter dog's breed: ");
read_line(new_node->breed, NAME_LEN);
printf("Enter owner's last name: ");
read_line(new_node->owner_last_name, NAME_LEN);
if(list == NULL)
{
list = new_node;
return list;
}
else{
for(cur = list; cur->next!= NULL; cur = cur->next);
cur->next = new_node;
new_node->next = NULL;
return list;
}
}
/***********************************************************
* search: Prompts the user to enter a dog's name, then *
* looks up dog(s) by name in the list. Prints the all the *
* informaiton of the dogs with the name if found. *
* Otherwise, prints a message. *
* ********************************************************/
void search (struct dog *list)
{
char search_name[NAME_LEN+1];
struct dog *p;
int found =0;
printf("Enter dog's name: ");
read_line(search_name, NAME_LEN);
for(p=list;
p != NULL;
p = p->next)
{
if(strcmp(search_name, p->dog_name)==0){
found = 1;
printf("%d ", p->number);
printf("%s ", p->dog_name);
printf("%s ", p->breed);
printf("%s ", p->owner_last_name);
}
}
if(!found)
printf("dog not found. ");
}
/************************************************************
* print: Prints a listing of all dogs in the list, showing *
* the dog's patient number, name, breed, and owner's last *
* name. *
* *********************************************************/
void print(struct dog *list){
struct dog *p;
printf("Dog Number Dog Name "
"Dog Breed Owner Last Name ");
for (p = list; p != NULL; p = p->next)
printf("%d %s %s %s ", p->number, p->dog_name,p->breed,
p->owner_last_name);
}
/***************************************************************
* clear: Clears the entire linked list. It begins at the head *
* of the list and frees memory allocated for each node of the *
* linked list. *
* ************************************************************/
void clear(struct dog *list)
{
struct dog *p;
while(list!=NULL)
{
p = list;
list = list->next;
if(p!=NULL)
free(p);
}
}
Readline.h
#ifndef read_lineh
#define read_lineh
#define NAME_LEN 30
int read_line(char str[], int n);
#endif
Readline.c:
#include
#include
#include
#include
#include"readline.h"
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;
}
Records.c
#include
#include
#include
#include
#include"readline.h"
#include"dogs.h"
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(" ");
}
}
Explanation / Answer
dog *delete_from_list(Dogs dog[])
{
char id; // This id will be the patient number of the dog.
printf("Enter patient ID to delete from the dogs record: ");
scanf("%d",&id);
total_size = sizeof(dog);
for(int i=0; i<total_size; i++)
{
if(dog[i].number = = id)
{
for(int j=i; j<total_size-1; j++)
{
dog[j].number = dog[j+1].number;
delete dog[i].number, dog[i].dog_name, dog[i].owner_last_name, dog[i].breed;
}
total_size--;
}
}
return dog;
}
/* After performing all the computation the dog should return the records to the
calling function.
*/
dog* append(dogs dog[])
{
printf("Insert Record ");
dog *theDog;
int len;
theDog = malloc(sizeof(dog)); //Allocating the size dyamically to create space while insertion.
if(!theDog)
{
printf("Unable to allocate memory for the structure. ");
return NULL;
}
for(int i=0;i<sizeof(dogs);i++)
{
theDog[i].number= number;
theDog[i].dog_name = dog_name;
theDog[i].owner_last_name = owner_last_name;
theDog[i].breed= breed;
}
theDog->next=NULL;
//Here we will sort the structure according to the name of the dog.
// We have used the concept of the Bubble sort here to sort the dog object.
for (i = 1; i < sizeof(dog); i++)
for (j = 0; j < sizeof(dog) - i; j++) {
if (strcmp(dog[j].dog_name, dog[j + 1].dog_name) > 0) {
temp = dog[j];
dog[j] = dog[j + 1];
dog[j + 1] = temp;
}
}
return theDog;
}
/* After performing all the computation the dog should return the records to the
calling function.
*/
//Modifying the records.c file to enable the delete feature into the executable code.
Records.c
#include
#include
#include
#include
#include"readline.h"
#include"dogs.h"
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;
case 'd' : delete(dog_list); //Added the delete function for the selection.
break;
default: printf("Illegal code ");
}
printf(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.