Answered You can hire a professional tutor to get the answer.

QUESTION

Need help in modifying the append_to_list function, given below, so a book is inserted into an ordered list (by author's last name and first name)

Need help in modifying the append_to_list function, given below, so a book is inserted into an ordered list (by author's 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("nEnter title:n");

 read_line(new_->title,TITLE_LEN);

 printf("Enter author first name:n");

 read_line(new_->first,NAME_LEN);

 printf("Enter author last name:n");

 read_line(new_->last,NAME_LEN);

 new_->next = NULL;

 if (list == NULL) 

 {

  printf("Enter price:n");

  scanf("%lf", &new_->price);

  printf("Enter number of requests:n");

  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: un");

   free(new_);

   return list;

  } 

  else 

  {

   prev = curr;

   curr = curr->next;

  }

 }

 printf("Enter price:n");

 scanf("%lf", &new_->price);

 printf("Enter number of requests:n");

 scanf("%d", &new_->num_requests);

 prev->next = new_;

 return list;

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question