In: Computer Science
The code following is what I have so far. It does not meet my requirements. My problem is that while this program runs, it doesn't let the user execute the functions of addBook, isInList or compareLists to add, check, or compare. Please assist in correcting this issue. Thank you!
Write a C++ program to implement a singly linked list of books. The book details should include the following: title, author, and ISBN. The program should include the following functions:
#include <iostream>
#include <string>
using namespace std;
struct Book {
string title;
string author;
string isbn;
Book(string t, string a, string is)
{
title = t;
author = a;
isbn = is;
}
Book* next;
};
class BookList {
private:
Book* head;
public:
BookList();
void addBook(Book* b);
bool isInList(string isbn);
bool compareLists(BookList& other);
};
BookList::BookList()
{
head = NULL;
}
void BookList::addBook(Book* b)
{
b->next = head;
head = b;
}
bool BookList::isInList(string isbn)
{
Book* temp = head;
while (temp != NULL) {
if (temp->isbn == isbn) {
return true;
}
temp = temp->next;
}
return false;
}
bool BookList::compareLists(BookList& other)
{
Book* temp = head;
while (temp != NULL) {
if (!other.isInList(temp->isbn)) {
return false;
}
temp = temp->next;
}
return true;
}
int main()
{
BookList list1, list2;
list1.addBook(new Book("title1", "author1", "isbn1"));
list1.addBook(new Book("title2", "author1", "isbn2"));
list1.addBook(new Book("title1", "author2", "isbn3"));
list1.addBook(new Book("title3", "author3", "isbn4"));
list2.addBook(new Book("title1", "author1", "isbn1"));
list2.addBook(new Book("title2", "author1", "isbn2"));
list2.addBook(new Book("title1", "author2", "isbn3"));
list2.addBook(new Book("title4", "author4", "isbn5"));
cout << "list1.isInList(\"isbn4\")" <<
list1.isInList("isbn4") << endl;
cout << "list1.isInList(\"isbn5\")" <<
list1.isInList("isbn5") << endl;
cout << "list1.isInList(\"isbn2\")" <<
list1.isInList("isbn2") << endl;
cout << "list1.compareLists(list2)" <<
list1.compareLists(list2) << endl;
return 0;
}
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.
Code:
#include <iostream>
#include <string>
using namespace std;
struct Book {
string title;
string author;
string isbn;
Book(string t, string a, string is)
{
title = t;
author = a;
isbn = is;
}
Book* next;
};
class BookList {
private:
Book* head;
public:
BookList();
void addBook(Book* b);
bool isInList(string isbn);
bool compareLists(BookList& other);
void printList();
};
BookList::BookList()
{
head = NULL;
}
void BookList::addBook(Book* b)
{
b->next = head;
head = b;
}
bool BookList::isInList(string isbn)
{
Book* temp = head;
while (temp != NULL) {
if (temp->isbn == isbn) {
return
true;
}
temp = temp->next;
}
return false;
}
void BookList::printList()
{
Book* temp = head;
while (temp != NULL) {
cout<<"Author
:"<<temp->author<<endl;
cout<<"ISBN
:"<<temp->isbn<<endl;
cout<<"Title
:"<<temp->title<<endl<<endl<<endl;
temp = temp->next;
}
}
bool BookList::compareLists(BookList& other)
{
Book* temp = head;
while (temp != NULL) {
if (!other.isInList(temp->isbn))
{
return
false;
}
temp = temp->next;
}
return true;
}
int showMenu(){
int choice;
cout<<"\n\nWhat do you want to
do?\n"<<endl;
cout<<"1: Add a book in first list.\n";
cout<<"2: Add a book in second list.\n";
cout<<"3: Search if book exist in first
list.\n";
cout<<"4: Search if book exist in second
list.\n";
cout<<"5: Compare two lists.\n";
cout<<"6: Print first list.\n";
cout<<"7: Print second list.\n";
cout<<"8: To exit\n";
cin>>choice;
cin.ignore();
cin.clear();
return choice;
}
Book* getBook(){
string author,isbn,title;
cout<<"Please give Title:";
getline(cin,title);
cout<<"Please give Author Name :";
getline(cin,author);
cout<<"Please give ISBN:";
getline(cin,isbn);
Book* obj=new Book(title, author, isbn);
return obj;
}
int main()
{
BookList list1, list2;
int choice=1;
Book* book;
string isbn;
while(choice!=8){
choice=showMenu();
switch (choice)
{
case 1:
book=getBook();
list1.addBook(book);
break;
case 2:
book=getBook();
list2.addBook(book);
break;
case 3:
cout<<"Please give isbn:\n";
cin>>isbn;
if(list1.isInList(isbn))
cout<<"This ISBN exists in list
1.\n";
else
cout<<"This ISBN does not exists in list
1.\n";
break;
case 4:
cout<<"Please give isbn:\n";
cin>>isbn;
if(list2.isInList(isbn))
cout<<"This ISBN exists in list
2.\n";
else
cout<<"This ISBN does not exists in list
2.\n";
break;
case 5:
if(list1.compareLists(list2))
cout<<"Both lists are same.\n";
else
cout<<"Lists are different.\n";
break;
case 6:
list1.printList();
break;
case 7:
list2.printList();
break;
case 8:
break;
default:
cout<<"Invalid choice!!!\n";
break;
}
}
/*\list1.addBook(new Book("title1", "author1",
"isbn1"));
list1.addBook(new Book("title2", "author1",
"isbn2"));
list1.addBook(new Book("title1", "author2",
"isbn3"));
list1.addBook(new Book("title3", "author3",
"isbn4"));
list2.addBook(new Book("title1", "author1",
"isbn1"));
list2.addBook(new Book("title2", "author1",
"isbn2"));
list2.addBook(new Book("title1", "author2",
"isbn3"));
list2.addBook(new Book("title4", "author4",
"isbn5"));
list1.printList();
cout << "list1.isInList(\"isbn4\")" <<
list1.isInList("isbn4") << endl;
cout << "list1.isInList(\"isbn5\")" <<
list1.isInList("isbn5") << endl;
cout << "list1.isInList(\"isbn2\")" <<
list1.isInList("isbn2") << endl;
cout << "list1.compareLists(list2)" <<
list1.compareLists(list2) << endl;*/
system("pause");
return 0;
}
Output:
What do you want to do? 1: Add a book in first list. 2: Add a book in second list. 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Please give Title:Title1 Please give Author Name : Authori Please give ISBN: isbn1 What do you want to do? 1: Add a book in first list. 2: Add a book in second list. 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Lists are different. What do you want to do? 1: Add a book in first list. 2: Add a book in second list. 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit
Please give Title:Title1 Please give Author Name : Authori Please give ISBN:isbn1 What do you want to do? 1: Add a book in first list. 2: Add a book in second list 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Both lists are same. What do you want to do? 1: Add a book in first list. 2: Add a book in second list 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Please give isbn: isbn1 This ISBN exists in list 1. What do you want to do? 1: Add a book in first list. 2: Add a book in second list 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list.
4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Author : Author1 ISBN :isbn1 Title :Title1 What do you want to do? 1: Add a book in first list. 2: Add a book in second list. 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Author : Authori ISBN :isbn1 Title :Title1 What do you want to do? 1: Add a book in first list. 2: Add a book in second list. 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit