Modify the append function so the dog is inserted into an ordered list (by dog n
ID: 3819838 • 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.
CODE PROVIDED BELOW:
#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;
};
/*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);
/**********************************************************
* 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(" ");
}
}
/************************************************************
* append: Prompts the user for informaiton about a dog and *
* then append the dog structure to the end of the list. *
* Prints an error message and returns prematurely if the *
* dog already exists by its patient number or memory *
* could not be allocated for the dog structure. *
* *********************************************************/
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{
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);
}
}
/***************************************************************
* read_line: Skips leading white-space characers, then reads *
* the remainder of the input line and stores it in str. *
* Truncate the line if its length exceeds n. Returns the *
* number of characters stored. *
* ************************************************************/
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
Answer:
The modifed program has been follows:
Program:
#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);
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)
{
struct dog *cur, *new_node;
new_node =(struct dog*) 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
cur = (struct dog*)malloc(sizeof(struct dog));
//if already there are some elements
if(list!=NULL)
{
//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;
}
}
//Finds a record withe the given dog name
void search(struct dog *list)
{
char dogName[NAME_LEN + 1];
printf("Enter the dog name: ");
read_line(dogName, NAME_LEN + 1);
struct dog * temp = list;
//Search each record
while (temp != NULL)
{
//if the record with the given name found, print the record
if (strcmp(temp->dog_name, dogName) == 0)
printf("Number: %d Dog name: %sBreed: %sOwners name: %s ", temp->number, temp->dog_name, temp->breed, temp->owner_last_name);
temp = temp->next;
}
}
//Displays all the records
void print(struct dog *list)
{
if (list == NULL)
{
printf("** No records found! **");
}
struct dog * temp = list;
//Print each record
while (temp != NULL)
{
printf("Number: %d Dog name: %s Breed: %s Owners name: %s ", temp->number, temp->dog_name, temp->breed, temp->owner_last_name);
temp = temp->next;
}
}
//De allocate the memory for each record
void clear(struct dog *list)
{
struct dog * temp = list;
while (temp != NULL)
{
temp = temp->next;
free(temp);
}
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.