Question
C++

In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a tit
addBook(): Adds a new book to the list. The new book is placed based on the ALPHABETICAL ORDER of the titles. You shouldnt a
print(string author): Prints all books of an author at(int index): Returns the book at given index getSize(): returns the num
Additional Resources: integer single linked list C++ class to help you to In addition, I have provided an study for linked li
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 Туре String String String Attribute title author 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 Туре Textbook* Attribute head Return Type Function constructor addBook(string title, string author, string ISBN) removeBook(string ISBN) print() print(char startingLetter) print(string author) at(int index) getSize() isEmpty() void void void void void Textbook int bool
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 C The Society of Mind Algorithms Programming Language 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.
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:
Additional Resources: integer single linked list C++ class to help you to In addition, I have provided an 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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below the Library.h file

#include<bits/stdc++.h>
using namespace std;

class Textbook{
   public:
       string title,author,ISBN;
       Textbook* next;
       Textbook(string t,string a,string i){
           this->title=t;
           this->author=a;
           this->ISBN=i;
           next=NULL;
       }
};

Below is the code of Library.cpp:

#include<bits/stdc++.h>
#include "Library.h"
using namespace std;
class library{
   public:
       Textbook *head=NULL;


       void addBook(string title,string author,string ISBN){
           Textbook *t;
           t=new Textbook(title,author,ISBN);
           if(head==NULL){
               head=t;
               return;
           }
           else{
               Textbook *temp,*prev=NULL;
               temp=head;
               while(temp!=NULL){
                   if(temp->title>=title)
                       break;
                   prev=temp;
                   temp=temp->next;
               }
               if(prev==NULL){
                   t->next=head;
                   head=t;
               }
               else{
                   prev->next=t;
                   t->next=temp;
               }
           }
       }
      
       void removeBook(string ISBN){
           Textbook *temp,*prev=NULL;
           temp=head;
           while(temp!=NULL){
               if(temp->ISBN==ISBN){
                   if(prev==NULL){
                       head=head->next;
                       return;
                   }
                   prev->next=temp->next;
                   return;
               }
               prev=temp;
               temp=temp->next;
           }
           throw "Error occured.";
       }
      
       void print(){
           Textbook *temp;
           temp=head;
           while(temp!=NULL){
               cout<<"Title: "<<temp->title<<" Author: "<<temp->author<<" ISBN: "<<temp->ISBN<<endl;
               temp=temp->next;
           }
       }
      
       void print(string author){
           Textbook *temp;
           temp=head;
           while(temp!=NULL){
               if(temp->author==author)
                   cout<<"Title: "<<temp->title<<" Author: "<<temp->author<<" ISBN: "<<temp->ISBN<<endl;
               temp=temp->next;
           }
       }
      
       void print(char start){
           Textbook *temp;
           temp=head;
           while(temp!=NULL){
               if(temp->author.at(0)==start){
                   cout<<"Title: "<<temp->title<<" Author: "<<temp->author<<" ISBN: "<<temp->ISBN<<endl;
               }
               temp=temp->next;
           }
       }
      
       Textbook* at(int index){
           if(index<0)
               return NULL;
           Textbook *temp=head;
           int i=0;
           while(i<index && temp!=NULL){
               temp=temp->next;
               ++i;
           }
           if(i<index)
               return NULL;
           return temp;
       }
      
       bool isEmpty(){
           if(head==NULL)
               return true;
           return false;
       }
      
       int getSize(){
           if(head==NULL)
               return 0;
           Textbook *temp;
           temp=head;
           int n=0;
           while(temp!=NULL){
               temp=temp->next;
               ++n;
           }
           return n;
       }
};

Sample Run:

library l;
   l.addBook("Algorithms","asdf","1234");
   l.addBook("The C Programming","fdge","3453");
   l.addBook("The Society of mind","asdf","4356");
   cout<<endl<<l.getSize()<<endl;
   l.print();

l.addBook("Applied","ertrh","454");
   l.print();

cout<<l.getSize()<<endl;


   l.print("fdge");

l.print('a');

l.removeBook("3453");
   l.print();
   cout<<endl<<l.getSize();

Output:

Title: Algorithms Author: asdf ISBN: 1234
Title: Applied Author: ertrh ISBN: 454
Title: The C Programming Author: fdge ISBN: 3453
Title: The Society of mind Author: asdf ISBN: 4356

4


Title: The C Programming Author: fdge ISBN: 3453

Title: Algorithms Author: asdf ISBN: 1234
Title: The Society of mind Author: asdf ISBN: 4356

Title: Algorithms Author: asdf ISBN: 1234
Title: Applied Author: ertrh ISBN: 454
Title: The Society of mind Author: asdf ISBN: 4356

3

Add a comment
Know the answer?
Add Answer to:
C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Ever...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please provide original Answer, I can not turn in the same as my classmate. thanks In...

    Please provide original Answer, I can not turn in the same as my classmate. thanks 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...

  • You need to program a simple book library system. There are three java classes Book.java   //...

    You need to program a simple book library system. There are three java classes Book.java   // book object class Library.java   //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

  • C++ Help. I am new to Visual Studio and C++ so please include comments! Please follow...

    C++ Help. I am new to Visual Studio and C++ so please include comments! Please follow the directions and make as simple as possible. Write a C++ program that reads the following list of input data (book.dat) and then allows users to search and view the books from a Menu List all available books Search for book using A. Title or B. ISBN? Exit Program The format of the file is as follows: //    The title of the book //   ...

  • Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors....

    Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors. Create Functions, include headers and other files, Loops(while, for), conditional(if, switch), datatypes,  etc. Assignment You work at the computer science library, and your boss just bought a bunch of new books for the library! All of the new books need to be cataloged and sorted back on the shelf. You don’t need to keep track of which customer has the book or not, just whether...

  • I wrote this code but there’s an issue with it : #include <iostream> #include <vector&...

    I wrote this code but there’s an issue with it : #include <iostream> #include <vector> #include <string> #include <fstream> using namespace std; class Borrower { private: string ID, name; public: Borrower() :ID("0"), name("no name yet") {} void setID(string nID); void setName(string nID); string getID(); string getName(); }; void Borrower::setID(string nID) { ID = nID; } void Borrower::setName(string nname) { name = nname; } string Borrower::getID() { return ID; } string Borrower::getName() { return name; } class Book { private: string...

  • Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After...

    Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to:  implement member functions  convert a member function into a standalone function  convert a standalone function into a member function  call member functions  implement constructors  use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...

  • Part 1 The purpose of this part of the assignment is to give you practice in...

    Part 1 The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following member functions (details about each one are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT