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

In this homework, you will implement a single linked list to store a list of com

ID: 3702580 • Letter: I

Question

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

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()

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:

                    The Algorithms -à The Programming Language-à The Society Mind

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 is

Empty(): 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.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This is the cpp file, zip file didn't work.

#include <iostream>

#include <fstream>

#include <string>

#include "Library.h"

using namespace std;

Library myLib;

//Read the dataset file and loads myLib list

void loadDataset(string fileName){

ifstream inFile;

inFile.open(fileName.c_str());

string line;

while(getline(inFile,line)){

int ind1 = line.find("#");

int ind2 = line.find("#",ind1+1);

string title = line.substr(0,ind1);

string author = line.substr(ind1+1, ind2-ind1-1);

string ISBN = line.substr(ind2+1);

//Here we add the new book. Please make sure your function name matches

myLib.addBook(title,author,ISBN);

}

}

//Just for convenience...

void continueMessage(string message){

cout << message << endl;

cout << "Press Enter to continue.." << endl; cin.get();

}

int main() {

string fileName = "dataset.txt";

loadDataset(fileName);

continueMessage("Dataset file is loaded to the program!");

//---------------------------------------------------------------------------

myLib.print();

continueMessage("All books in the library are listed!");

//---------------------------------------------------------------------------

string title = "Total Recall: How the E-Memory Revolution Will Change Everything";

string author = "C. Gordon Bell";

string ISBN = "592968760-9";

myLib.addBook(title,author,ISBN);

continueMessage("New book, with " + ISBN + " ISBN is added to the library.");

//---------------------------------------------------------------------------

myLib.print(title[0]); string s(1, title[0]);

continueMessage("The books, whose title starts with "+s+", are listed.");

//---------------------------------------------------------------------------

author = "W. Richard Stevens";

myLib.print(author);

continueMessage("The books, whose author is "+author+", are listed.");

//---------------------------------------------------------------------------

myLib.removeBook(ISBN);

continueMessage("The book, with " + ISBN + " ISBN is removed from the library.");

//---------------------------------------------------------------------------

Textbook tb = myLib.at(5);

cout << "Textbook at index 5: " << tb.title << " " << tb.author << " " << tb.ISBN << endl;

//---------------------------------------------------------------------------

int size = myLib.getSize();

cout << "There are currently " << size << " books in the library." << endl;

return 0;

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

data set

Introduction to Algorithms#Thomas H. Cormen#878134263-2
Structure and Interpretation of Computer Programs#Harold Abelson#336190405-6
The C Programming Language#Brian W. Kernighan#699022731-1
The Pragmatic Programmer: From Journeyman to Master#Andy Hunt#888099490-5
The Art of Computer Programming, Volumes 1-3 Boxed Set#Donald Ervin Knuth#625268126-1
Design Patterns: Elements of Reusable Object-Oriented Software#Erich Gamma#604854500-2
Introduction to the Theory of Computation#Michael Sipser#615228728-6
Code: The Hidden Language of Computer Hardware and Software#Charles Petzold#822491420-8
The Mythical Man-Month: Essays on Software Engineering#Frederick P. Brooks Jr.#987411743-5
Artificial Intelligence: A Modern Approach#Stuart Russell#704058716-5
Code Complete#Steve McConnell#465609758-6
The Protocols (TCP/IP Illustrated, Volume 1)#W. Richard Stevens#126456048-6
Algorithms#Robert Sedgewick#873746894-4
Advanced Programming in the UNIX Environment#W. Richard Stevens#598255768-4
A Discipline of Programming#Edsger W. Dijkstra#412861075-5
Introduction to Automata Theory, Languages, and Computation#John E. Hopcroft#952883775-1
Compilers: Principles, Techniques, and Tools#Alfred V. Aho#557104773-9
Joel on Software#Joel Spolsky#394368902-6
Learn You a Haskell for Great Good!: A Beginner's Guide#Miran Lipova a#260995077-2
The Society of Mind#Marvin Minsky#539224508-0

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Additional Resources

#include <iostream>

using namespace std;

class Node{

public:

int value;

Node* next;

Node(int newVal){

value = newVal;

next = NULL;

}

};

class LinkedList{

private:

Node* head;

int _size;

public:

LinkedList(){_size=0;}

LinkedList(int newVal){

head = new Node(newVal);

_size = 1;

}

int size(){ return _size;}

bool isEmpty(){return (_size==0)?true:false;}

void pushBack(int newValue){

//Create the node to be added

Node* newElement = new Node(newVal);

//Find the node that has a NULL next pointer. (Last node)

Node* current = head;

while(current->next != NULL){

current = current->next;

}

current->next = newElement;

_size++;

}

void insertAt(int newValue,int index){

//The index must be in the range

if(index <= _size && index >= 0){

//Create the node to be added

Node* newElement = new Node(newVal);

//If the node is added at the beginning we have a special case

if(index == 0){

newElement->next = head;

head = newElement;

}

else{

Node* current = head;

for(int i = 0;i<index-1;i++)

current = current->next;

newElement->next = current->next;

current->next = newElement;

}

_size++;

}

else{

cout << newValue << " cannot be added. Index " << index << " is out of range" << endl;

}

}

//Access the element in given index

int at(int index){

if(index < _size && index >= 0){

Node* current = head;

for(int i = 0;i<index;i++)

current = current->next;

return current->value;

}

else{

cout << "Index " << index << " is out of range." << endl;

}

}

int removeAt(int index){

if(index < _size && index >= 0){

//If the node is added at the beginning we have a special case

Node* deletedNode;

if(index == 0){

deletedNode = head;

head = head->next;

}

else{

Node* current = head;

for(int i = 0;i<index-1;i++)

current = current->next;

deletedNode = current->next;

current->next = current->next->next;

}

int deletedVal = deletedNode->value;

delete deletedNode;

_size--;

return deletedVal;

}

else{

cout << "Index " << index << " is out of range." << endl;

return -1;

}

}

//print the list

void print(){

Node* current = head;

while(current!=NULL){

cout << current->value << " ";

current = current->next;

}

cout << endl;

}

};

int main(){

LinkedList myList(5);

myList.pushBack(4); myList.print();

myList.pushBack(1); myList.print();

myList.pushBack(3); myList.print();

myList.pushBack(7); myList.print();

myList.insertAt(8,2); myList.print(); //Add to ind = 2

myList.insertAt(2,0); myList.print(); //Add to the beginning

myList.insertAt(6,myList.size()); myList.print(); //Add to the end

int val;

val = myList.removeAt(0); cout << val << " removed! -> "; myList.print();

val = myList.removeAt(2); cout << val << " removed! -> ";myList.print();

val = myList.removeAt(myList.size()-1); cout << val << " removed! -> ";myList.print();

if(myList.isEmpty())

cout << "The list is Empty" << endl;

else

cout << "The list is NOT Empty" << endl;

for(int i=0;i<myList.size();i++)

cout << "List[" << i << "] = " << myList.at(i) << endl;

cout << "There are " << myList.size() << " elements in the list." << endl;

return 0;

}

Textbook

Type

Attribute

String

title

String

author

String

ISBN

Textbook*

next

Explanation / Answer

PROGRAM CODE:

#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 << " ";

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote