Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

book_requests program: #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

image text in transcribed

book_requests program:

#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);

/**********************************************************

* 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 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

str[i++] = ch;

}

str[i] = '\0';

return i;

}

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, author's 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: Ask the user to enter a book's title, author's first name, author's last name (in the exact order) Check whether the book has already existed by title and author's name. If the book has the same title and author's 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. 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 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 to the linked list. b. c. d. update: update a book's number of requests. Ask the user to enter the book's title, author's 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. 2. 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. Note: use read_line function included in the program for reading in title, author first name, and last name

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Repairing And Querying Databases Under Aggregate Constraints

Authors: Sergio Flesca ,Filippo Furfaro ,Francesco Parisi

2011th Edition

146141640X, 978-1461416401

More Books

Students also viewed these Databases questions