Question
Modify the append_to_list function so a book is inserted into an ordered list (by authors last name and first name) and the list remains ordered
Modify the append_to_list function so a book is inserted into an ordered list (by authors last name and first name) and the list remains ordered after the insertion. For example, a book by Ashley Spires should be before a book by Whee Winn in the list; a book by Elizabeth Spires should be after a book by Ashely Spires in the list. The order of the books does not matter if they have the same author.
struct book *append_to_list(struct book *list) { struct book *prev = NULL, *curr = list; struct book *new_ = NULL;
new_ = malloc(sizeof(struct book)); printf(" Enter title: "); read_line(new_->title,TITLE_LEN); printf("Enter author first name: "); read_line(new_->first,NAME_LEN); printf("Enter author last name: "); read_line(new_->last,NAME_LEN); new_->next = NULL; if (list == NULL) { printf("Enter price: "); scanf("%lf", &new_->price); printf("Enter number of requests: "); scanf("%d", &new_->num_requests); return new_; } while (curr != NULL) { if ((strcmp(new_->title, curr->title)== 0) &&(strcmp(new_->first, curr->first)==0) && (strcmp(new_->last, curr->last)==0)) { printf("book already exists. To update number of requests, enter operation code: u "); free(new_); return list; } else { prev = curr; curr = curr->next; } } printf("Enter price: "); scanf("%lf", &new_->price); printf("Enter number of requests: "); scanf("%d", &new_->num_requests); prev->next = new_; return list; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started