Question: // CSE240 Fall 2018 HW6 // Write your name here // Write the compiler used: Visual studio or gcc // READ BEFORE YOU START: //
// CSE240 Fall 2018 HW6 // Write your name here // Write the compiler used: Visual studio or gcc // READ BEFORE YOU START: // You are given a partially completed program that creates a linked list of books, say for library database. // Each book has the this information: book name, publication year, library name, number of copies of that book. // The struct 'bookDetails' holds information of one book. Note that library name is enum type. // A linked list called 'list' is made to hold a 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. // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. // ***** WRITE COMMENTS FOR IMPORANT STEPS OF YOUR CODE. ***** // ***** GIVE MEANINGFUL NAMES TO VARIABLES. ***** #include #include #include #include // needed to use tolower() #pragma warning(disable: 4996) // for Visual Studio Only #define MAX_NAME_LENGTH 35 typedef enum { noble = 0, hayden } library; // enumeration type library struct bookDetails { char bookName[MAX_NAME_LENGTH]; int pubYear; library libName; int noOfCopies; struct bookDetails* next; // pointer to next node } *list = NULL; // Declare linked list 'list' // forward declaration of functions (already implmented) void flushStdIn(); void executeAction(char); // functions that need implementation: int addNode(char* bookName_input, int pubYear_input, char* libName_input, int noOfCopies_input); // 10 points void displayList(); // 10 points int countNodes(); // 5 points int deleteNode(char* bookName_input); // 10 points void swapNodes(struct bookDetails* node1, struct bookDetails* node2); // 5 points void sortList(); // 10 points int main() { char choice = 'i'; // initialized to a dummy value do { printf(" CSE240 HW6 "); printf("Please enter your selection: "); printf("\t a: add a new book to the list "); printf("\t d: display entire list of books "); printf("\t r: remove a book "); printf("\t s: sort the list by book name "); printf("\t q: quit "); choice = tolower(getchar()); flushStdIn(); executeAction(choice); } while (choice != 'q'); 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 // Read the function case by case void executeAction(char c) { char bookName_input[MAX_NAME_LENGTH]; unsigned int pubYear_input, noOfCopies_input, result= 0; char libName_input[10]; switch (c) { case 'a': // add book // input book details from user printf(" Please enter book name: "); fgets(bookName_input, sizeof(bookName_input), stdin); bookName_input[strlen(bookName_input) - 1] = '\0'; // discard the trailing ' ' char printf("Please enter publication year: "); scanf("%d", &pubYear_input); flushStdIn(); printf("Please enter library name (noble/hayden): "); fgets(libName_input, sizeof(libName_input), stdin); libName_input[strlen(libName_input) - 1] = '\0'; // discard the trailing ' ' char printf("Please enter no. of copies of book: "); scanf("%d", &noOfCopies_input); flushStdIn(); // add the book to the list result = addNode(bookName_input, pubYear_input, libName_input, noOfCopies_input); if (result == 0) printf(" That book is already on the list! "); else printf(" Book successfully added to the list! "); break; case 'd': // display the list displayList(); break; case 'r': // remove book printf(" Please enter book name: "); fgets(bookName_input, sizeof(bookName_input), stdin); bookName_input[strlen(bookName_input) - 1] = '\0'; // discard the trailing ' ' char result = deleteNode(bookName_input); if (result == 0) printf(" Book does not exist or the list is empty! "); else printf(" Book successfully removed from the list! "); break; case 's': // sort the list sortList(); break; case 'q': // quit break; default: printf("%c is invalid input! ",c); } } // Q1 : addNode (10 points) // This function is used to insert a new book into the list. You can simply insert the new book to the end of the linked list. // Do not allow the book to be added to the list if it already exists in the list. You can do that by checking the names of the books already in the list. // If the book already exists then return 0 without adding book to the list. If the book does not exist in the list then go on to add the book at the end of the list and return 1. // NOTE: You must convert the string 'libName_input' to an enum type and store it in the list because the struct has enum type for library name. int addNode(char* bookName_input, int pubYear_input, char* libName_input, int noOfCopies_input) { struct bookDetails* tempList = list; // work on a copy of 'list' return 0; // remove this line while implementing this funtion // It is added to avoid compile error for empty function } // Q2 : displayList (10 points) // This function displays the linked list of books with the book details (struct elements) of each book. // Parse through the linked list and print the book details one after the other. See expected output screenshots in homework question file. // NOTE: Library name is stored in the struct as enum type. You need to display 'Noble' or 'Hayden' as library name for the book. void displayList() { struct bookDetails* tempList = list; // work on a copy of 'list' } // Q3: countNodes (5 points) // This function counts the number of books on the linked list and returns the number. // You may use this function in other functions like deleteNode(), sortList(), addNode(). // Its helpful to call this function in other functions, but not necessary. int countNodes() { return 0; // remove this line while implementing this funtion // It is added to avoid compile error for empty function } // Q4 : deleteNode (10 points) // This function deletes the node specified by the book name. // Parse through the linked list and remove the node containing this book name. // NOTE: After you remove the node, make sure that your linked list is not broken. // (Hint: Visualize the list as: list......-> node1 -> removed_node -> node2 -> ...end. // After removing "removed_node", the list is broken into "list ....node1" and "node2.... end". Stitch these lists.) int deleteNode(char* bookName_input) { // create a copy of 'list' to parse the list return 0; // remove this line while implementing this funtion // It is added to avoid compile error for empty function } // Q5 : swapNodes (5 points) // This function swaps the elements of parameters 'node1' and 'node2' (not the first and second node of 'list') // This function should not alter the 'next' element of the nodes. // NOTE: This function is to be used in the sorting logic in sortList() // Hint: The swap logic is similar to swapping two simple integer/string variables void swapNodes(struct bookDetails* node1, struct bookDetails* node2) { } // Q6 : sortList (10 points) // This function sorts the linked list by alphabetical order of book name. // Parse the list and compare the book names 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 linked list of books having sorted books. // Hint: One of the string library function can be useful to implement this function because the sorting needs to happen by book name which is a string. // Use swapNodes() to swap the nodes in the sorting logic void sortList() { struct bookDetails* tempList = list; // work on a copy of 'list' }



CSE240 HW6 lease enter your selection: a: add a new book to the list d: display entire list of books r: remove a book s: sort the list by book name q: quit Book name: Intro to prog lang ublication year: 2016 ibrary: Noble Book name: java programming ublication year: 2005 ibrary: Noble Book name: Computer organization ublication year: 2009 ibrary: Noble No. of copies : 5 CSE240 HW6 lease enter your selection: a: add a new book to the list d: display entire list of books r: remove a book s: sort the list by book name q: quit Book name: Intro to prog lang ublication year: 2016 ibrary: Noble Book name: java programming ublication year: 2005 ibrary: Noble Book name: Computer organization ublication year: 2009 ibrary: Noble No. of copies : 5
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
