Modify the append function so the dog is inserted into an ordered list (by dog n
ID: 3692097 • 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.
Append function:
struct dog *append(struct dog *list){
struct dog *p, *a;
p= malloc(sizeof(struct dog));
if(p==NULL) {
printf("Cannot add more dogs");
return list;
}
printf("Enter the patient's number:");
scanf("%d ",&p->number);
for(a= list; a!=NULL; a=a->next)
if(a!=NULL && p->number==a->number) {
printf("Patient already exists");
free(p);
return list;
}
printf("Enter the dog's name:");
read_line(p->dog_name,NAME_LEN);
printf("Enter the dog's breed");
read_line(p->breed,NAME_LEN);
printf("Enter the owner's last name:");
read_line(p->owner_last_name,NAME_LEN);
if(list==NULL) {
list=p;
return list;
}
else {
for(a=list; a->next!=NULL;a=a->next);
a->next=p;
p->next=NULL;
return list;
}
}
All i need is for this function to be modified as required
Explanation / Answer
Answer:
The modified program is as follows:
Program:
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 there are no elements in the list
if(list == NULL)
{
list = new_node;
return list;
}
//The append code till now is same as your now we need to make the
//changes to incorporate the additional features given in the problem
struct dog * cur = (struct dog*)malloc(sizeof(struct dog));
//if already there are some elements
else
{
//initialising two structures for storing purposes
struct dog * temp2 = (struct dog*)malloc(sizeof(struct dog));
struct dog * temp3 = (struct dog*)malloc(sizeof(struct dog));
//Assigning the list to the cur and temp2
cur=temp2=list;
//for travesing entire list
while(cur!=NULL)
{
//if detect a dog name in the list that is alphabetically
//greater than our new node value
//so we need to insert it in proper position
//if loop to handle such a situation
if (strcmp(cur->dog_name,new_node->dog_name)>=0)
{
//if new node dog name less than a list node dog name
if (strcmp(cur->dog_name,new_node->dog_name)>0)
{
// inserting the new node before the detected node cur
cur=temp2->next;
temp2->next=new_node;
new_node->next=cur;
break;
}
//if dog names are same
else
{
//checking for owners last name
//if owners last name is less than detected node
if (strcmp(cur->owner_last_name,new_node->owner_last_name)>0)
{
//inserting dog before the detected node cur
cur=temp2->next;
temp2->next=new_node;
new_node->next=cur;
break;
}
//else if owners last name is greater than the detected node cur
else
{
//inserting after the detected node cur
new_node->next=cur->next;
cur->next=new_node;
break;
}
}
}
// if reached lat node with out any alphabetical order violation
//insert the new node at the end
if(cur->next == NULL)
{
cur->next=new_node;
break;
}
//temp2 to store the previous node of cur node
temp2=cur;
//incrementing cur to next node to run the while loop entirely
cur=cur->next;
}
//returning list
return list;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.