Question

In: Computer Science

Two sorted lists have been created, one implemented using a linked list (LinkedListLibrary linkedListLibrary) and the...

Two sorted lists have been created, one implemented using a linked list (LinkedListLibrary linkedListLibrary) and the other implemented using the built-in Vector class (VectorLibrary vectorLibrary). 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 the respective LinkedListLibrary and VectorLibrary InsertSorted() methods and outputting the number of operations the computer must perform to insert the new book. Each InsertSorted() returns the number of operations the computer performs.

Ex: If the input is:

The Catcher in the Rye
9787543321724
J.D. Salinger

the output is:

Number of linked list operations: 1
Number 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 <fstream>
#include <iostream>
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 if fails
}

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 LinkedListLibrary destructor
currNode = new BookNode(bookTitle, bookAuthor, bookISBN);
// TODO: Call LL_Library's InsertSorted() method to insert currNode and return
// the number of operations performed

linkedListLibrary.lastNode = currNode;

// Insert into VectorList
tempBook = Book(bookTitle, bookAuthor, bookISBN);
// TODO: Call VectorLibrary's InsertSorted() method to insert currNode 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 <iostream>

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, int counter) {
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() < newNode->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 <iostream>

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, int counter) {
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() < newNode->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 <iostream>

VectorLibrary::VectorLibrary() {
vector<Book> library;
}

int VectorLibrary::InsertSorted(const Book &newBook, int counter) {
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 (empty slot),
// 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 <string>
using namespace std;

class BookNode {
public:
BookNode();

// Constructor
BookNode(string bookTitleInit, string bookAuthorInit, long bookISBNInit);

// Constructor
BookNode(string bookTitleInit, string bookAuthorInit, long bookISBNInit, 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 <iostream>

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 <string>
using namespace std;

class Book{
public:
Book();

Book(string userBookTitle, string userBookAuthor, long userBookISBN);

long GetBookISBN() const;

void PrintInfo() const;

private:
string bookTitle;
string bookAuthor;
long bookISBN;
};

#endif

_______________________________________________

Book.cpp

#include "Book.h"
#include <iostream>

Book::Book() {
bookTitle = "";
bookAuthor = "";
bookISBN = 0;
}

Book::Book(string userBookTitle, string userBookAuthor, long userBookISBN) {
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;
}

Solutions

Expert Solution

Solution:

Code Screenshots:

Sample Input File (“books.txtâ€):

The Latcher in the Rye

9787543321719

P.D. Slinger

The Hatcher in the Rye

9787543321722

F.D. Langer

The Batcher in the Rye

9787543321721

T.D. Ginger

The Pitcher in the Rye

9787543321723

M.D. Ringer

The Patcher in the Rye

9787543321714

S.D

Sample Output:

Code to Copy:

main.cpp:

#include "LinkedListLibrary.h"

#include "VectorLibrary.h"

#include "BookNode.h"

#include "Book.h"

#include <fstream>

#include <iostream>

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 if fails

}

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 LinkedListLibrary destructor

currNode = new BookNode(bookTitle, bookAuthor, bookISBN);

// TODO: Call LL_Library's InsertSorted() method to insert currNode and return

// the number of operations performed

linkedListOperations = linkedListLibrary.InsertSorted(currNode, linkedListOperations);

linkedListLibrary.lastNode = currNode;

// Insert into VectorList

tempBook = Book(bookTitle, bookAuthor, bookISBN);

// TODO: Call VectorLibrary's InsertSorted() method to insert currNode and return

// the number of operations performed

vectorOperations = vectorLibrary.InsertSorted(tempBook, vectorOperations);

// TODO: Print number of operations for linked list

cout <<"Number of linked list operations: "

<< linkedListOperations << endl;

// TODO: Print number of operations for vector

cout <<"Number of vector operations: "

<< vectorOperations << endl;

}

Book.h:

#ifndef BOOKH

#define BOOKH

#include <string>

using namespace std;

class Book{

public:

Book();

Book(string userBookTitle, string userBookAuthor, long userBookISBN);

long GetBookISBN() const;

void PrintInfo() const;

private:

string bookTitle;

string bookAuthor;

long bookISBN;

};

#endif

Book.cpp:

#include "Book.h"

#include <iostream>

Book::Book() {

bookTitle = "";

bookAuthor = "";

bookISBN = 0;

}

Book::Book(string userBookTitle, string userBookAuthor, long userBookISBN) {

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;

}

BookNode.h:

#ifndef BOOKNODEH

#define BOOKNODEH

#include <string>

using namespace std;

class BookNode {

public:

BookNode();

// Constructor

BookNode(string bookTitleInit, string bookAuthorInit, long bookISBNInit);

// Constructor

BookNode(string bookTitleInit, string bookAuthorInit, long bookISBNInit, 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 <iostream>

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;

}

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 <iostream>

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, int counter) {

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

#ifndef VECTORLIBRARYH

#define VECTORLIBRARYH

#include "Book.h"

#include <vector>

using namespace std;

class VectorLibrary {

public:

VectorLibrary();

int InsertSorted(const Book &newBook, int counter);

void PrintLibrary() const;

private:

// vector library

vector<Book> library;

};

#endif

VectorLibrary.cpp:

#include "VectorLibrary.h"

#include <iostream>

VectorLibrary::VectorLibrary() {

vector<Book> library;

}

int VectorLibrary::InsertSorted(const Book &newBook, int counter) {

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 (empty slot),

// 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;

}

}

____________________

Happy learning

Please give upvote.


Related Solutions

C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Working with Linked Lists in C++ Tasks As stated in the objectives, you have two methods...
Working with Linked Lists in C++ Tasks As stated in the objectives, you have two methods to implement. These methods (which contain TODO comments) are found in linked_list.cpp. Part 1 Implement the copy constructor; use the LinkedBag as inspiration for your solution as the technique of creating a deep copy is essentially the same. Part 2 Implement the replace method. //linked_list.cpp #include "linked_list.h" // Header file #include <cassert> template<class Object> LinkedList<Object>::LinkedList() : headPtr( nullptr ), itemCount( 0 ) { }...
how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface can be found below: List.java public interface List<T> extends Iterable<T> { /** * Insert an element at a specified location. * @param index * @param obj * @throws IndexOutOfBoundsException */ public void add(int index, T obj); /** * Append an object to the end of the list. * @param obj */ public boolean add(T obj); public void clear(); public boolean contains(T obj); /** *...
In C++ In this lab we will creating two linked list classes: one that is a...
In C++ In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT