Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Just need Q2 this is using a linked list not an array. // READ BEFORE YOU START: // You are given a partially completed program

Just need Q2 this is using a linked list not an array.

// READ BEFORE YOU START: // You are given a partially completed program that creates a list of books, like book's record. // Each record has this information: book title, author, type of the book, book id and aisle in which the book is to be kept. // The struct 'libraryRecord' holds information of one book. bookType is enum type. // An array of structs called 'list' is made to hold the list of books. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // You should not modify any of the given code, the return types, or the parameters, you risk getting compile error. // You are not allowed to modify main (). // You can use string library functions.

// WRITE COMMENTS FOR IMPORANT STEPS IN YOUR CODE.

#include #include #include #pragma warning(disable: 4996) // for Visual Studio Only

#define MAX_BOOKS 15 #define MAX_NAME_LENGTH 25

typedef enum { Nonfiction = 0, Fiction } bookType; // enum type

struct libraryRecord { // struct for book details char bookTitle[MAX_NAME_LENGTH]; char author[MAX_NAME_LENGTH]; bookType booktype; unsigned int bookId; unsigned int aisle; };

struct libraryRecord list[MAX_BOOKS]; // declare list of books int count = 0; // the number of books currently stored in the list (initialized to 0)

// functions already implmented void flushStdIn(); void executeAction(char); void save(char* fileName);

// functions that need implementation: int add(char* bookTitle_input, char* author_input, char* booktype_input, unsigned int bookId_input, unsigned int aisle_input); // 20 points void sort(); // 10 points int delete(unsigned int bookId_input); // 10 points void load(char* fileName); // 10 points void display(); // given

int main() { char* fileName = "Book_List.txt"; load(fileName); // load list of books from file (if it exists). Initially there will be no file. char choice = 'i'; // initialized to a dummy value do { printf(" Enter your selection: "); printf("\t a: add a new book "); printf("\t d: display book list "); printf("\t r: remove a book from list "); printf("\t s: sort book list by ID "); printf("\t q: quit "); choice = getchar(); flushStdIn(); executeAction(choice); } while (choice != 'q');

save(fileName); // save list of books to file (overwrites file, if it exists) return 0; }

// flush out leftover ' ' characters void flushStdIn() { char c; do c = getchar(); while (c != ' ' && c != EOF); }

// ask for details from user for the given selection and perform that action void executeAction(char c) { char bookTitle_input[MAX_NAME_LENGTH], author_input[MAX_NAME_LENGTH]; unsigned int bookId_input, aisle_input, add_result = 0; char booktype_input[20]; switch (c) { case 'a': // input book record from user printf(" Enter book title: "); fgets(bookTitle_input, sizeof(bookTitle_input), stdin); bookTitle_input[strlen(bookTitle_input) - 1] = '\0'; // discard the trailing ' ' char printf("Enter author name: "); fgets(author_input, sizeof(author_input), stdin); author_input[strlen(author_input) - 1] = '\0'; // discard the trailing ' ' char printf("Enter book type (Fiction/ Nonfiction): "); fgets(booktype_input, sizeof(booktype_input), stdin); booktype_input[strlen(booktype_input) - 1] = '\0'; // discard the trailing ' ' char printf("Please enter book ID number: "); scanf("%d", &bookId_input); printf("Please enter aisle number: "); scanf("%d", &aisle_input); flushStdIn();

// add the book to the list add_result = add(bookTitle_input, author_input, booktype_input, bookId_input, aisle_input); if (add_result == 0) printf(" Book is already on the list! "); else if (add_result == 1) printf(" Book successfully added to the list! "); else printf(" Unable to add. Book list is full! ");

break; case 'r': printf("Please enter ID number of book to be deleted: "); scanf("%d", &bookId_input); flushStdIn(); int delete_result = delete(bookId_input); if (delete_result == 0) printf(" Book not found in the list! "); else printf(" Book deleted successfully! "); break; case 'd': display(); break; case 's': sort(); break;

case 'q': break; default: printf("%c is invalid input! ", c); } }

// Q2 : sort (10 points) // This function is used to sort the list(array of structs) numerically by book ID. // Parse the list and compare the book IDs to check which one should appear before the other in the list. // Sorting should happen within the list. That is, you should not create a new array of structs having sorted books. // Hint: Use a temp struct (already declared) if you need to swap two structs in your logic void sort() { struct libraryRecord libraryTemp; // needed for swapping structs. Not absolutely necessary to use. // Enter code here

// display message for user to check the result of sorting. printf(" Book list sorted! Use display option 'd' to view sorted list. "); }

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

DB2 11 The Ultimate Database For Cloud Analytics And Mobile

Authors: John Campbell, Chris Crone, Gareth Jones, Surekha Parekh, Jay Yothers

1st Edition

1583474013, 978-1583474013

More Books

Students also viewed these Databases questions