Question: 1. You are given a partially completed program hw06q1.c. The structure of this homework is similar to previous homework. In this homework, you should use
1. You are given a partially completed program hw06q1.c. The structure of this homework is similar to previous homework. In this homework, you should use linked list to do the same work in the previous homework. You should follow the instructions given in the program to complete the functions so that the program executes properly. You will be completing a program that creates a linked list of books. It is a menu-driven program where the user is given the following options: CSE240 Introduction to Programming Language 2 | Page Homework 06 a) Add a new book to the linked list. When adding a new book to the list, the user is prompted for book name, publication year, library name and number of copies of the book. The book should be added at the end of the linked list. If the name of the book to add already exists in the list, then you should not add the book to the list. The library name is enum type. b) Display the list of books. c) Remove the book from the linked list. After removing the book, the linked list should not be broken. d) Sort the linked list of books alphabetically by book name. The sorting should happen within the list. You should not create a new linked list of books having sorted books. You also need to implement a swap function which is to be used in the sort function.


----------------------------------------------------------------------------------- // 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' }
SE240 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 ease enter book name: Intro to prog lang lease enter publication year: 2016 lease enter library name (noble/hayden): noble lease enter no. of copies of book: 10 ook successfully added to the list! CSE240 Introduction to Programming Language Homework 06 displayList: CSE240 HW6 Please 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 Publication year: 2016 Library: Noble No. of copies: 10 Book name: java programming Publication year: 2005 Library: Noble lo. of copies: 4 Book name: Computer organization Publication year: 2009 Library: 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
