Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> //

ID: 3756112 • Letter: #

Question


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>  // 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(" a: add a new book to the list ");
  printf(" d: display entire list of books ");
  printf(" r: remove a book ");
  printf(" s: sort the list by book name ");
  printf(" 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] = ''; // 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] = ''; // 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] = ''; // 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'

}

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 Homework 06 1 | Page 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 Display the list of books b) 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 Expected output of each menu option: (same as previous homework) addNode CSE240 H16 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 lease 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!

Explanation / Answer

Given below is the completed code for the question.
Please do rate the answer if it was helpful. Thank you

// CSE240 Fall 2018 HW5
// 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 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.
// An array of structs 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
// because you are not allowed to modify main ().
// You can use string library functions.
// WRITE COMMENTS FOR IMPORANT STEPS OF YOUR CODE.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // needed to use tolower()
#pragma warning(disable: 4996) // for Visual Studio Only
#define MAX_BOOKS 20
#define MAX_NAME_LENGTH 35
typedef enum { noble = 0, hayden } library; // enumeration type library
struct bookDetails {
char bookName[MAX_NAME_LENGTH];
int pubYear; // publication year
library libName;
int noOfCopies;
};
struct bookDetails list[MAX_BOOKS]; // declare list of books
int count = 0; // the number of books currently stored in the list (initialized at 0)
// forward declaration of functions (already implmented)
void flushStdIn();
void executeAction(char);
void save(char* fileName);
// functions that need implementation:
int add(char* bookName_input, int pubYear_input, char* libName_input, int noOfCopies_input); // 20 points
void display(); // 10 points
void sort(); // 10 points
void load(char* fileName); // 10 points
int main()
{
load("Book_List.txt"); // 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(" CSE240 HW5 ");
printf("Please enter your selection: ");
printf(" a: add a new book to the list ");
printf(" d: display entire list of books ");
printf(" s: sort books by name ");
printf(" q: quit ");
choice = tolower(getchar());
flushStdIn();
executeAction(choice);
} while (choice != 'q');
save("Book_List.txt"); // 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 bookName_input[MAX_NAME_LENGTH];
unsigned int pubYear_input, noOfCopies_input, add_result= 0;
char libName_input[10];
switch (c)
{
case 'a':
// input book details from user
printf(" Please enter book name: ");
fgets(bookName_input, sizeof(bookName_input), stdin);
bookName_input[strlen(bookName_input) - 1] = ''; // 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] = ''; // discard the trailing ' ' char
printf("Please enter no. of copies of book: ");
scanf("%d", &noOfCopies_input);
flushStdIn();
// add the book to the list
add_result = add(bookName_input, pubYear_input, libName_input, noOfCopies_input);
if (add_result == 0)
printf(" That book is already on the list! ");
else
printf(" Book successfully added to the list! ");
break;
case 's': sort(); break;
case 'd': display(); break;
case 'q': break;
default: printf("%c is invalid input! ",c);
}
}
// Q1 : add (20 points)
// This function is used to insert a new book into the list. You can simply insert the new book to the end of list (array of structs).
// 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 it 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.
// You can assume that user will input library name in lowercase.
// Hint: 'count' holds the number of books currently in the list
int add(char* bookName_input, int pubYear_input, char* libName_input, int noOfCopies_input)
{
int i;
for(i = 0; i < count; i++)
{
if(strcmp(list[i].bookName, bookName_input) == 0) //already exists
return 0;
}
strcpy(list[count].bookName, bookName_input);
list[count].pubYear = pubYear_input;
list[count].noOfCopies = noOfCopies_input;
if(strcmp(libName_input, "noble") == 0)
list[count].libName = noble;
else
list[count].libName = hayden;
count++;
return 1;
}
// Q2 : display (10 points)
// This function displays the list of books with the book details (struct elements) of each book.
// Parse through the book 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 display()
{
int i;
printf("%-40s %-20s %-10s %-10s ", "Book Name", "Publishing Year", "Library", "Copies");
for(i = 0; i < count; i++){
if(list[i].libName == noble)
printf("%-40s %-20d %-10s %-10d ", list[i].bookName, list[i].pubYear, "Noble", list[i].noOfCopies);
else
printf("%-40s %-20d %-10s %-10d ", list[i].bookName, list[i].pubYear, "Hayden", list[i].noOfCopies);
}
printf(" ");
}
// Q3 : sort (10 points)
// This function is used to sort the book list alphabetically by book name.
// Parse the book 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 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 a temp struct if you need to swap two book structs in your logic
void sort()
{
struct bookDetails bookTemp; // needed for swapping book structs. Not absolutely necessary to use.
// enter your code here
int i , j, minIdx;
//selection sort
for(i = 0; i < count; i++)
{
minIdx = i;
for(j = i + 1; j < count; j++)
{
if(strcmp(list[j].bookName, list[minIdx].bookName) < 0)
minIdx = j;
}
if(i != minIdx)//need to swap
{
bookTemp = list[i];
list[i] = list[minIdx];
list[minIdx] = bookTemp;
}
}
// display message for user to check the result of sorting.
printf(" Book list sorted! Use display option 'd' to view sorted list. ");
}
// save() is called at the end of main()
// This function saves the array of structures to file. It is already implemented.
// You should understand how this code works so that you know how to use it for future homework.
// It will also help you with 'load' function.
void save(char* fileName)
{
FILE* file;
int i, libnameValue=0;
file = fopen(fileName, "wb");
fwrite(&count, sizeof(count), 1, file); // First, store the number of books in the list
// Parse the list and write book details to file1
//
for (i = 0; i < count; i++)
{
fwrite(list[i].bookName, sizeof(list[i].bookName), 1, file);
fwrite(&list[i].pubYear, sizeof(list[i].pubYear), 1, file);
// convert enum to a number for storing
if (list[i].libName == noble)
{
libnameValue = 0; // 0 for noble
}
else
libnameValue = 1; // 1 for hayden
fwrite(&libnameValue, sizeof(libnameValue), 1, file);
fwrite(&list[i].noOfCopies, sizeof(list[i].noOfCopies), 1, file);
}
fclose(file);
}
// Q4: load (10 points)
// This function is called in the beginning of main().
// This function reads the book list from the saved file and builds the array of structures 'list'.
// In the first run of the program, there will be no saved file because save() is done at the end of program.
// So at the begining of this function, write code to open the file and check if it exists. If file does not exist, then return from the function.
// (See expected output of add() in homework question file. It displays "Book_List.txt not found" because the file did not exist initially.)
// If the file exists, then parse the book list to write the book details to the file.
// Use the save function given above as an example of how to write this function. Notice the order of elements that is used to write to file in save()
// You need to use the same order to read the list back.
// NOTE: The saved file is not exactly readable because all elements of the struct are not string or char type.
// So you need to implement load() similar to how save() is implemented. Only then the 'list' will be loaded correctly.
void load(char* fileName)
{
FILE* file;
int i, libnameValue=0;
file = fopen(fileName, "rb");
if(file == NULL)
return;
fread(&count, sizeof(count), 1, file); // First, store the number of books in the list
for (i = 0; i < count; i++)
{
fread(list[i].bookName, sizeof(list[i].bookName), 1, file);
fread(&list[i].pubYear, sizeof(list[i].pubYear), 1, file);
fread(&libnameValue, sizeof(libnameValue), 1, file);
// convert enum to a number for storing
if (libnameValue == 0)
{
list[i].libName = noble; // 0 for noble
}
else
list[i].libName = hayden; // 1 for hayden
fread(&list[i].noOfCopies, sizeof(list[i].noOfCopies), 1, file);
}
fclose(file);
}

output
------

CSE240 HW5
Please enter your selection:
a: add a new book to the list
d: display entire list of books
s: sort books by name
q: quit
a
Please enter book name: C programming
Please enter publication year: 1990
Please enter library name (noble/hayden): noble
Please enter no. of copies of book: 5
Book successfully added to the list!

CSE240 HW5
Please enter your selection:
a: add a new book to the list
d: display entire list of books
s: sort books by name
q: quit
a
Please enter book name: Java Programming
Please enter publication year: 2015
Please enter library name (noble/hayden): hayden
Please enter no. of copies of book: 4
Book successfully added to the list!

CSE240 HW5
Please enter your selection:
a: add a new book to the list
d: display entire list of books
s: sort books by name
q: quit
d
Book Name Publishing Year Library Copies
C programming 1990 Noble 5
Java Programming 2015 Hayden 4

CSE240 HW5
Please enter your selection:
a: add a new book to the list
d: display entire list of books
s: sort books by name
q: quit
a
Please enter book name: C++ programming
Please enter publication year: 2000
Please enter library name (noble/hayden): noble
Please enter no. of copies of book: 3
Book successfully added to the list!

CSE240 HW5
Please enter your selection:
a: add a new book to the list
d: display entire list of books
s: sort books by name
q: quit
d
Book Name Publishing Year Library Copies
C programming 1990 Noble 5
Java Programming 2015 Hayden 4
C++ programming 2000 Noble 3

CSE240 HW5
Please enter your selection:
a: add a new book to the list
d: display entire list of books
s: sort books by name
q: quit
s
Book list sorted! Use display option 'd' to view sorted list.
CSE240 HW5
Please enter your selection:
a: add a new book to the list
d: display entire list of books
s: sort books by name
q: quit
d
Book Name Publishing Year Library Copies
C programming 1990 Noble 5
C++ programming 2000 Noble 3
Java Programming 2015 Hayden 4

CSE240 HW5
Please enter your selection:
a: add a new book to the list
d: display entire list of books
s: sort books by name
q: quit
q
===========================================
$ ./a.out
CSE240 HW5
Please enter your selection:
a: add a new book to the list
d: display entire list of books
s: sort books by name
q: quit
d
Book Name Publishing Year Library Copies
C programming 1990 Noble 5
C++ programming 2000 Noble 3
Java Programming 2015 Hayden 4

CSE240 HW5
Please enter your selection:
a: add a new book to the list
d: display entire list of books
s: sort books by name
q: quit
q