Two sorted lists have been created, one implemented using alinked list (LinkedListLibrary linkedListLibrary) and the otherimplemented using the built-in Vector class (VectorLibraryvectorLibrary). Each list contains 100 books (title, ISBN number,author), sorted in ascending order by ISBN number.
Complete main() by inserting a new book into each list using therespective LinkedListLibrary and VectorLibrary InsertSorted()methods and outputting the number of operations the computer mustperform to insert the new book. Each InsertSorted() returns thenumber of operations the computer performs.
Ex: If the input is:
The Catcher in the Rye9787543321724J.D. Salinger
the output is:
Number of linked list operations: 1Number of vector operations: 1
________________________________________
The given code that i must use is:
____________________________________________________________________
Main.cpp
#include \"LinkedListLibrary.h\"
#include \"VectorLibrary.h\"
#include \"BookNode.h\"
#include \"Book.h\"
#include
#include
using namespace std;
void FillLibraries(LinkedListLibrary &linkedListLibrary,VectorLibrary &vectorLibrary) {
ifstream inputFS; // File input stream
int linkedListOperations = 0;
int vectorOperations = 0;
BookNode* currNode;
Book tempBook;
string bookTitle;
string bookAuthor;
long bookISBN;
// Try to open file
inputFS.open(\"books.txt\");
while(getline(inputFS, bookTitle)) {
inputFS >> bookISBN;
inputFS.ignore(1, '\n');
getline(inputFS, bookAuthor);
// Insert into linked list
currNode = new BookNode(bookTitle, bookAuthor, bookISBN);
linkedListOperations = linkedListLibrary.InsertSorted(currNode,linkedListOperations);
linkedListLibrary.lastNode = currNode;
// Insert into vector
tempBook = Book(bookTitle, bookAuthor, bookISBN);
vectorOperations = vectorLibrary.InsertSorted(tempBook,vectorOperations);
}
inputFS.close(); // close() may throw ios_base::failure iffails
}
int main (int argc, const char* argv[]) {
int linkedListOperations = 0;
int vectorOperations = 0;
// Create libraries
LinkedListLibrary linkedListLibrary = LinkedListLibrary();
VectorLibrary vectorLibrary;
// Fill libraries with 100 books
FillLibraries(linkedListLibrary, vectorLibrary);
// Create new book to insert into libraries
BookNode* currNode;
Book tempBook;
string bookTitle;
string bookAuthor;
long bookISBN;
getline(cin, bookTitle);
cin >> bookISBN;
cin.ignore();
getline(cin, bookAuthor);
// Insert into linked list
// No need to delete currNode, deleted by LinkedListLibrarydestructor
currNode = new BookNode(bookTitle, bookAuthor, bookISBN);
// TODO: Call LL_Library's InsertSorted() method to insert currNodeand return
// the number of operations performed
linkedListLibrary.lastNode = currNode;
// Insert into VectorList
tempBook = Book(bookTitle, bookAuthor, bookISBN);
// TODO: Call VectorLibrary's InsertSorted() method to insertcurrNode and return
// the number of operations performed
// TODO: Print number of operations for linked list
// TODO: Print number of operations for vector
}
__________________________________________________
LinkedListLibrary.h
#ifndef LINKEDLISTLIBRARYH
#define LINKEDLISTLIBRARYH
#include \"BookNode.h\"
using namespace std;
class LinkedListLibrary {
public:
//Linked list nodes
BookNode* headNode;
BookNode* lastNode;
LinkedListLibrary();
~LinkedListLibrary();
int InsertSorted(BookNode* newNode, int counter);
void PrintLibrary() const;
};
#endif
________________________________________________________
LinkedListLibrary.cpp
#include \"LinkedListLibrary.h\"
#include
LinkedListLibrary::LinkedListLibrary() {
// Front of nodes list
headNode = nullptr;
lastNode = nullptr;
}
LinkedListLibrary::~LinkedListLibrary() {
while(headNode != nullptr) {
BookNode* tempNode = headNode->GetNext();
delete headNode;
headNode = tempNode;
}
}
int LinkedListLibrary::InsertSorted(BookNode* newNode, intcounter) {
BookNode* currNode, nextNode;
// Special case for head node
if (headNode == nullptr || headNode->GetBookISBN() >=newNode->GetBookISBN()) {
newNode->SetNext(headNode);
headNode = newNode;
}
else {
// Locate the node before insertion point
currNode = headNode;
while (currNode->GetNext() &&currNode->GetNext()->GetBookISBN() GetBookISBN()) {
currNode = currNode->GetNext();
}
currNode->insertAfter(newNode);
}
++counter;
return counter;
}
void LinkedListLibrary::PrintLibrary() const {
BookNode* currNode;
currNode = headNode->GetNext();
while (currNode != nullptr) {
currNode->PrintBookInfo();
cout << endl;
currNode = currNode->GetNext();
}
}
____________________________________________________
VectorLibrary.h
#include \"LinkedListLibrary.h\"
#include
LinkedListLibrary::LinkedListLibrary() {
// Front of nodes list
headNode = nullptr;
lastNode = nullptr;
}
LinkedListLibrary::~LinkedListLibrary() {
while(headNode != nullptr) {
BookNode* tempNode = headNode->GetNext();
delete headNode;
headNode = tempNode;
}
}
int LinkedListLibrary::InsertSorted(BookNode* newNode, intcounter) {
BookNode* currNode, nextNode;
// Special case for head node
if (headNode == nullptr || headNode->GetBookISBN() >=newNode->GetBookISBN()) {
newNode->SetNext(headNode);
headNode = newNode;
}
else {
// Locate the node before insertion point
currNode = headNode;
while (currNode->GetNext() &&currNode->GetNext()->GetBookISBN() GetBookISBN()) {
currNode = currNode->GetNext();
}
currNode->insertAfter(newNode);
}
++counter;
return counter;
}
void LinkedListLibrary::PrintLibrary() const {
BookNode* currNode;
currNode = headNode->GetNext();
while (currNode != nullptr) {
currNode->PrintBookInfo();
cout << endl;
currNode = currNode->GetNext();
}
}
________________________________________________________________________
VectorLibrary.cpp
#include \"VectorLibrary.h\"
#include
VectorLibrary::VectorLibrary() {
vector library;
}
int VectorLibrary::InsertSorted(const Book &newBook, intcounter) {
Book currBook;
// Add an empty element at end of list
Book emptyBook;
library.push_back(emptyBook);
// Loop through elements starting at the end
for (int i = library.size() - 2; i >=0; --i) {
currBook = library.at(i);
// If the current book's ISBN is larger than newBook's ISBN,shift
// the current book down 1, count shift operation
if(currBook.GetBookISBN() > newBook.GetBookISBN()){
library.at(i + 1) = currBook;
++counter;
}
// Otherwise, place newBook at the next location (emptyslot),
// count insert operation
else {
library.at(i + 1) = newBook;
++counter;
return counter;
}
}
// If we get to the top of the list, place newBook on top
library.at(0) = newBook;
++counter;
return counter;
}
void VectorLibrary::PrintLibrary() const {
for (size_t i = 0; i < library.size(); ++i) {
library.at(i).PrintInfo();
cout << endl;
}
}
_____________________________________________
BookNode.h
#ifndef BOOKNODEH
#define BOOKNODEH
#include
using namespace std;
class BookNode {
public:
BookNode();
// Constructor
BookNode(string bookTitleInit, string bookAuthorInit, longbookISBNInit);
// Constructor
BookNode(string bookTitleInit, string bookAuthorInit, longbookISBNInit, BookNode* nextLoc);
// inserAfter
void insertAfter(BookNode* nodeLoc);
 Â
//setNext
void SetNext(BookNode* nodeLoc);
// Get location pointed by nextNodePtr
BookNode* GetNext() const;
long GetBookISBN() const;
// Print book information
void PrintBookInfo() const;
private:
string bookTitle;
string bookAuthor;
long bookISBN;
BookNode* nextNodePtr; // Reference to the next node
};
#endif
_________________________________________-
BookNode.cpp
#include \"BookNode.h\"
#include
BookNode::BookNode() {
bookTitle = \"\";
bookAuthor = \"\";
bookISBN = 0;
nextNodePtr = nullptr;
}
// Constructor
BookNode::BookNode(string bookTitleInit, string bookAuthorInit,long bookISBNInit) {
bookTitle = bookTitleInit;
bookAuthor = bookAuthorInit;
bookISBN = bookISBNInit;
nextNodePtr = nullptr;
}
// Constructor
BookNode::BookNode(string bookTitleInit, string bookAuthorInit,long bookISBNInit, BookNode* nextLoc) {
bookTitle = bookTitleInit;
bookAuthor = bookAuthorInit;
bookISBN = bookISBNInit;
nextNodePtr = nextLoc;
}
// insertAfter
void BookNode::insertAfter(BookNode* nodeLoc){
BookNode* tmpNext;
tmpNext = nextNodePtr;
nextNodePtr = nodeLoc;
nodeLoc->nextNodePtr = tmpNext;
}
// setNext
void BookNode::SetNext(BookNode* nodeLoc) {
nextNodePtr = nodeLoc;
}
// Get location pointed by nextNodePtr
BookNode* BookNode::GetNext() const{
return nextNodePtr;
}
long BookNode::GetBookISBN() const{
return bookISBN;
}
// Print book information
void BookNode::PrintBookInfo() const{
cout << \"Title: \" << bookTitle << endl;
cout << \"Author: \" << bookAuthor << endl;
cout << \"ISBN: \" << bookISBN << endl;
}
_______________________________________________
Book.h
#ifndef BOOKH
#define BOOKH
#include
using namespace std;
class Book{
public:
Book();
Book(string userBookTitle, string userBookAuthor, longuserBookISBN);
long GetBookISBN() const;
void PrintInfo() const;
private:
string bookTitle;
string bookAuthor;
long bookISBN;
};
#endif
_______________________________________________
Book.cpp
#include \"Book.h\"
#include
Book::Book() {
bookTitle = \"\";
bookAuthor = \"\";
bookISBN = 0;
}
Book::Book(string userBookTitle, string userBookAuthor, longuserBookISBN) {
bookTitle = userBookTitle;
bookAuthor = userBookAuthor;
bookISBN = userBookISBN;
}
long Book::GetBookISBN() const{
return bookISBN;
}
void Book::PrintInfo() const{
cout << \"Title: \" << bookTitle << endl;
cout << \"Author: \" << bookAuthor << endl;
cout << \"ISBN: \" << bookISBN << endl;
}