Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the append function so the dog is inserted into an ordered list (by dog n

ID: 3822555 • Letter: M

Question

Modify the append function so the dog is inserted into an ordered list (by dog name and owner last name) and the list remains ordered after the insertion. For example, a dog named Buddy with owner’s last name White should be after Buddy with owner’s last name Martin but before a dog named Max with owner’s last name White in the list

here is another example :

Number Name       Breed       Owner Last Name'
19 Max Bulldog Johnson
27 Lucy Boxer Jones
35 Max       Bulldog           White
31 Molly chihuahua White

Dog.c Code

/**************
*Dogs.c *
***************/

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#include <stdlib.h>

#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);

new_node->next = NULL;

if(list == NULL)

{

list = new_node;

return list;

}

elsse

{

for(cur = list; cur->next!= NULL; cur = cur->next);

cur->next = new_node;

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);

}

}

Explanation / Answer

Hi Friend, Since you have not posted dogs.h file, So I can not test program.

I have implemented required function and please let me know in case of any issue.

/**************
*Dogs.c *
***************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#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);
new_node->next = NULL;
if(list == NULL)
{
list = new_node;
return list;
}
else
{
  
// inserting at first position if new_node is less than head node
if(strcmp(list->dog_name, new_node->dog_name) > 0){
new_node->next = list;
list = new_node;
return list;
}

struct dog *prev = NULL;
curr = list;
while((strcmp(curr->next->dog_name, new_node->dog_name) <= 0) ||
((strcmp(curr->next->dog_name, new_node->dog_name) == 0) &&
(strcmp(curr->next->owner_last_name, new_node->owner_last_name) <= 0))){
prev = curr;
curr = curr->next;
}

new_node->next = curr->next;
curr->next = new_node;

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);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote