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

CS3330 Homework 3 (Due: 4/9/2018 by 11:59PM) In this homework, you will implemen

ID: 3703649 • Letter: C

Question

CS3330 Homework 3

(Due: 4/9/2018 by 11:59PM)

In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a “next” pointer.

Textbook

Type

Attribute

String

title

String

author

String

ISBN

Textbook*

next

Library class should have a head node as an attribute to keep the list of the books. Also, following member functions should be implemented in this class:

Library

Type

Attribute

Textbook*

head

Return Type

Function

constructor

void

addBook(string title, string author, string ISBN)

void

removeBook(string ISBN)

void

print()

void

print(char startingLetter)

void

print(string author)

Textbook

at(int index)

int

getSize()

bool

isEmpty()

addBook(): Adds a new book to the list. The new book is placed based on the ALPHABETICAL ORDER of the titles. You shouldn’t add the textbook directly to the end or front of the list. The new book has to be inserted to the correct position. For example, if you have following three books:

A new book, “Applied Cryptography” will be placed in alphabetical order between the first and second book.

removeBook(): This functions will remove a book using the given ISBN number. If the given ISBN number is not in the list, it will give an error.

print(): Print all books in (alphabetical) order. Title, author, and the ISBN should be printed for each book.

print(char startingLetter): Prints all books whose title starts with the input character, “startingLetter”.

print(string author): Prints all books of an author. at(int index): Returns the book at given index getSize(): returns the number of books in the list isEmpty(): returns true if list is empty, returns false otherwise.

The main program (main.cpp) is provided for you. So you will only implement the Textbook and library classes. I expect you to have 2 files: Library.h and Library.cpp. Textbook class definition will be in the Library.h file.

main.cpp includes all necessary functions to read the dataset file (dataset.txt). Also, several test cases are prepared for you to see whether your code is running or not. You do not need to change any code in the main.cpp file. Whenever you include your library class and main cpp to your project, they must run properly together.

Additional Resources:

In addition, I have provided an integer single linked list C++ class to help you to study for linked lists. If you study that code, you can see the following functions:

void pushBack(int newValue) void insertAt(int newValue,int index) int at(int index) int removeAt(int index) void print()  

This code is given you for a reference not to use it in this assignment. There are some functions in that class that you don’t need or there are some which requires some modifications.

Explanation / Answer

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

struct book

{

string title;

string author;

string publisher;

int year;

string isbn;

book* next;

book(string, string, string, int, string, book*);

};

book::book(string tempTitle, string tempAuthor, string tempPublisher, int tempYear, string tempIsbn, book* tempNext)

:title(tempTitle), author(tempAuthor), publisher(tempPublisher), year(tempYear), isbn(tempIsbn), next(tempNext)

{}

typedef book* bookPtr;

void getline(istream &stream, string &str, char delimiter)

{ char temp[300];

stream.get(temp, 300, delimiter);

stream.ignore(300, delimiter);

str = temp;

}

void getline(istream &stream, int &num, char delimiter)

{ int temp;

stream >> temp;

stream.ignore(300, delimiter);

num= temp;

}

void readFile(bookPtr &root);

void insert (bookPtr &root);

void delTitle(bookPtr &root);

bookPtr locateNode(bookPtr temp, string tit);

void delIsbn(bookPtr &root);

bookPtr locateNodeIsbn(bookPtr temp, string isb);

void searchIsbn(bookPtr temp);

void printList(bookPtr temp);

void printAuthor(bookPtr temp);

void saveFile(bookPtr temp);

int countNodes(bookPtr temp);

int main()

{

int choice;

bookPtr root = NULL;

readFile(root);

do

{

cout << "Menu: Select your option ";

cout << "(1) Add a book to the list ";

cout << "(2) Delete a book based on Title ";

cout << "(3) Delete a book based on ISBN ";

cout << "(4) Search for a book by ISBN. ";

cout << "(5) List all books. ";

cout << "(6) List all books by an author. ";

cout << "(7) Quit. ";

cout << "Enter your choice ---> ";

cin >> choice;

if (1 <= choice && choice <= 6)

{

switch (choice)

{

case 1:

insert(root);

break;

case 2:

delTitle(root);

break;

case 3:

delIsbn(root);

break;

case 4:

searchIsbn(root);

break;

case 5:

printList(root);

break;

case 6:

printAuthor(root);

break;

default:

cout << "Invalid choice. Enter again. ";

break;

}

}

}

while (choice != 7);

saveFile(root);

return 0;

}

void readFile(bookPtr &root)

{

int numBooks, yea;

string tit, aut, pub, isb;

ifstream infile ("books.txt", ios::in);

infile >> numBooks;

infile.ignore(300,' ');

for (int count = 0; count < numBooks; count++)

{

getline(infile, tit, ' ');

getline(infile, aut, ' ');

getline(infile,pub, ' ');

getline(infile,yea, ' ');

getline(infile, isb, ' ');

root = new book (tit, aut, pub, yea, isb, root);

}

}

void insert (bookPtr &root)

{

string tit, aut, pub, isb;

int yea;

cout << "Title: ";

cin.ignore(300,' ');

getline(cin, tit, ' ');

cout << "Author: ";

getline(cin, aut, ' ');

cout << "Publisher: ";

getline(cin,pub, ' ');

cout << "Year: ";

getline(cin,yea, ' ');

cout << "ISBN: ";

getline(cin, isb, ' ');

root = new book (tit, aut, pub, yea, isb, root);

}

bookPtr locateNode(bookPtr temp, string tit)

{

while (temp != NULL)

{

if (temp->title == tit)

{

return temp;

}

temp = temp->next;

}

return NULL;

}

void delIsbn(bookPtr &root)

{

string isb;

cout << "Book ISBN: ";

cin.ignore(300,' ');

getline(cin, isb, ' ');

bookPtr p = locateNodeIsbn(root, isb);

if (p == NULL)

cout << " Deletion cannot be done. ";

else if (root == p)

root = p->next;

else

{

bookPtr q = root;

while (q->next != p)

q = q->next;

q->next = p->next;

}

delete p;

}

bookPtr locateNodeIsbn(bookPtr temp, string isb)

{

while (temp != NULL)

{

if (temp->isbn == isb)

{

return temp;

}

temp = temp->next;

}

return NULL;

}

void delTitle(bookPtr &root)

{

string tit;

cout << "Book Title: ";

cin.ignore(300,' ');

getline(cin, tit, ' ');

bookPtr p = locateNode(root, tit);

if (p == NULL)

cout << " Deletion cannot be done. ";

else if (root == p)

root = p->next;

else

{

bookPtr q = root;

while (q->next != p)

q = q->next;

q->next = p->next;

}

delete p;

}

void searchIsbn(bookPtr temp)

{

string isb;

cout << "Book ISBN: ";

cin.ignore(300,' ');

getline(cin, isb, ' ');

while (temp != NULL)

{

if (isb == temp->isbn)

{

cout << temp->title << " ";

cout << temp->author << " ";

cout << temp->publisher << " ";

cout << temp->year << " ";

cout << temp->isbn << " ";

}

temp = temp->next;

}

cout << " ";

}

void printAuthor(bookPtr temp)

{

string aut;

cout << "Author name: ";

cin.ignore(300,' ');

getline(cin, aut, ' ');

while (temp != NULL)

{

if (temp->author == aut)

{

cout << temp->title << " ";

cout << temp->author << " ";

cout << temp->publisher << " ";

cout << temp->year << " ";

cout << temp->isbn << " ";

}

temp = temp->next;

}

cout << " ";

}

void printList(bookPtr temp)

{

while (temp != NULL)

{

cout << temp->title << " ";

cout << temp->author << " ";

cout << temp->publisher << " ";

cout << temp->year << " ";

cout << temp->isbn << " ";

temp = temp->next;

}

cout << " ";

}

int countNodes(bookPtr temp)

{

int countB = 0;

while (temp != NULL)

{

countB++;

temp = temp->next;

}

return countB;

}

void saveFile(bookPtr temp)

{

int count = countNodes(temp);

ofstream outFile("saved.txt",ios::out);

outFile << count << " ";

while (temp != NULL)

{

outFile << temp->title << " ";

outFile << temp->author << " ";

outFile << temp->publisher << " ";

outFile << temp->year << " ";

outFile << temp->isbn << " ";

temp = temp->next;

}

cout << " ";

}