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: 3699479 • 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 title ISBN Type Attribute String String String Textbook* author 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

Explanation / Answer

//Library.h

#ifndef LIBRARY_H_

#define LIBRARY_H_

# include <iostream>

# include <string>

# include <stdlib.h>

using namespace std;

class Textbook{

               public:

                              string title;

                              string author;

                              string ISBN;

                              Textbook *next;

                              Textbook(string title,string author,string ISBN);

};

class Library{

               private:

                              Textbook* head;

               public:

                              Library();

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

};

#endif /* LIBRARY_H_ */

//end of Library.h

//Library.cpp

# include "Library.h"

Textbook::Textbook(string title,string author,string ISBN)

{

               this->title = title;

               this->author = author;

               this->ISBN = ISBN;

               this->next = NULL;

}

Library::Library()

{

               head = NULL;

}

void Library ::addBook(string title,string author,string ISBN)

{

               Textbook* newBook = new Textbook(title,author,ISBN);

               if(head == NULL)

                              head = newBook;

               else if(newBook->title.compare(head->title) < 0)

               {

                              newBook->next=head;

                              head = newBook;

               }else{

                              bool inserted = false;

                              Textbook *curr = head;

                              Textbook *preCurr = NULL;

                              while(curr != NULL)

                              {

                                             if(curr->title.compare(newBook->title) > 0)

                                             {

                                                            newBook->next=curr;

                                                            preCurr->next= newBook;

                                                            inserted = true;

                                                            break;

                                             }

                                             preCurr = curr;

                                             curr = curr->next;

                              }

                              if(!inserted)

                              {

                                             preCurr->next=newBook;

                              }

               }

}

void Library :: removeBook(string ISBN)

{

               if(head->ISBN.compare(ISBN) == 0)

               {

                              Textbook* temp = head;

                              head = head->next;

                              delete(temp);

                              return ;

               }

               Textbook *curr = head;

               Textbook* preCurr = NULL;

               while(curr != NULL)

               {

                              if(curr->ISBN.compare(ISBN) == 0)

                              {

                                             preCurr->next = curr->next;

                                             delete(curr);

                                             return ;

                              }

                              preCurr = curr;

                              curr = curr->next;

               }

               cerr<<" No such ISBN exists"<<endl;

}

void Library:: print()

{

               if(head == NULL)

               {

                              cout<<" Empty list"<<endl;

                              return;

               }

               Textbook* temp = head;

               while(temp != NULL)

               {

                              cout<<" ISBN :"<<temp->ISBN<<" Title : "<<temp->title<<" Author : "<<temp->author<<endl;

                              temp = temp->next;

               }

}

void Library:: print(char startingLetter)

{

               if(head == NULL)

               {

                              cout<<" Empty list"<<endl;

                              return;

               }

               Textbook* temp = head;

               while(temp != NULL)

               {

                              if(toupper(temp->title.at(0)) == toupper(startingLetter))

                              {

                                             cout<<" ISBN :"<<temp->ISBN<<" Title : "<<temp->title<<" Author : "<<temp->author<<endl;

                              }

                              temp = temp->next;

               }

}

void Library :: print(string author)

{

               if(head == NULL)

               {

                              cout<<" Empty list"<<endl;

                              return;

               }

               Textbook* temp = head;

               while(temp != NULL)

               {

                              if(temp->author.compare(author) == 0)

                              {

                                             cout<<" ISBN :"<<temp->ISBN<<" Title : "<<temp->title<<" Author : "<<temp->author<<endl;

                              }

                              temp = temp->next;

               }

}

Textbook Library :: at(int index)

{

               if(head == NULL){

                              Textbook tb("","","");

                              return tb;

               }

               Textbook* temp = head;

               int i=0;

               while(temp != NULL)

               {

                              if(i == index){

                                             Textbook tb(temp->title,temp->author,temp->ISBN);

                                             return tb;

                              }

                              temp = temp->next;

                              i++;

               }

               Textbook tb("","","");

               return tb;

}

int Library ::getSize()

{

               int size =0;

               Textbook* temp = head;

               while(temp != NULL)

               {

                              temp = temp->next;

                              size++;

               }

               return size;

}

bool Library:: isEmpty()

{

               return (head == NULL);

}

//end of Library.cpp

//main.cpp

#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 add the new book

                              myLib.addBook(title,author,ISBN);

               }

}

void continueMessage(string message)

{

               cout<<message<<endl;

               cout<<"Please enter to continue.."<<endl;

               cin.get();

}

int main()

{

               string filename = "dataset.txt";

               loadDataset(filename);

               continueMessage("Dataset file is loaded into 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;

}

//end of main.cpp

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