Question: Please use C language only. An elementary school teacher would like to maintain a list of book requests from the students for the classroom library.
Please use C language only.
An elementary school teacher would like to maintain a list of book requests from the students for the classroom library. Each book was stored with the title, authors first name, last name, price, and number of requests. The program book_requests.c contains the book struct declaration, function prototypes, and the main function. Complete the function definitions so it uses a dynamically allocated linked list to store the book requests. Complete the following functions:
1.append_to_list:
a. Ask the user to enter a books title, authors first name, authors last name (in the exact order).
b. Check whether the book has already existed by title and authors name. If the book has the same title and authors name with an existing book in the list, the function should print a message about using the update function to update the number of requests and exit.
c. If the book does not exist, ask the user to enter price and number of requests, allocate memory for it, store the data, and append it to the end of the linked list.
d. If the list is empty, the function should return the pointer to the newly created linked list. Otherwise, add the book to the end of the linked list and return the pointer tothelinkedlist.
2. update: update a books number of requests. Ask the user to enter the books title, authors first name, and last name. Find the matching book, ask the user to enter the number of requests to be added and update the number of requests. If the book is not found, print a message.
3.printList: print the title, price, and number of request of all books in the list.
4.clearList: when the user exists the program, all the memory allocated for the linked list should be deallocated.
Use read_line function included in the program for reading in title, author first name, and last name.
Here is program outline:
#include
#include
#include
#include
#define TITLE_LEN 100
#define NAME_LEN 30
struct book{
char title[TITLE_LEN+1];
char first[NAME_LEN+1];
char last[NAME_LEN+1];
double price;
int num_requests;
struct book *next;
};
struct book *append_to_list(struct book *list);
void update(struct book *list);
void printList(struct book *list);
void clearList(struct book *list);
int read_line(char str[], int n);
int main(void)
{
char code;
struct book *book_list = NULL;
printf("Operation Code: a for appending to the list, u for updating a
book"
", 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': book_list = append_to_list(book_list);
break;
case 'u': update(book_list);
break;
case 'p': printList(book_list);
break;
case 'q': clearList(book_list);
return 0;
default: printf("Illegal code ");
}
printf(" ");
}
}
struct book *append_to_list(struct book *list){
//add your code
return NULL;
}
void update(struct book *list)
{
//add your code
}
void printList(struct book *list){
//add your code
}
void clearList(struct book *list)
{
//add your code
}
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] = '\0';
return i;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
