Question

Modify the booklist program (from the lecture notes) by adding delet and insert operations into the...

Modify the booklist program (from the lecture notes) by adding delet and insert operations into the BookList class. The insert method should be based on a compareTo() method in the Book class that determines if one book title comes before another alphabetically. The insert method will maintain the BookList in alphabetical order. Exercise various insertion and deletion operations, using the code below for your main program:

//lib.cpp

#include “pch.h”

#include

using namespace std;

#include "BookList.h"

int main(int argc, char* argv[]) {

//--------------------------------------------------------------

// Creates a BookList object, adds several books to the list,

// then prints them.

//--------------------------------------------------------------

   char list[LIST_LEN];

   BookList *books = new BookList();

   books->insert (new Book("F Title") );

   books->insert (new Book("D Title"));

   books->insert (new Book("G Title"));

   books->insert (new Book("A Title"));

   books->insert (new Book("E Title"));

   books->insert (new Book("H Title"));

   cout << "After inserts:\n”;

   cout << books->getBookList(list) << endl;;

   books->delet (new Book("A Title"));

   books->delet (new Book("H Title"));

   books->delet (new Book("G Title"));

   books->delet (new Book("E Title"));

   cout << "After deletes:\n";

   cout << books->getBookList(list) << endl;;

   books->insert (new Book("A Title"));

   books->insert (new Book("E Title"));

   books->insert (new Book("H Title"));

   books->insert (new Book("G Title"));

   cout << "After 2nd inserts:\n”;

   cout << books->getBookList(list) << endl;;

return 0;

}

///////////////////////////////////////////////////////////////////////////////////////////////////

_________________________________________________

My Header Files

book.h

#pragma once
#include
#include
#define TITLE_LEN 81

using namespace std;

//********************************************************************
// Book.cpp
//
// Represents a single book.
//*******************************************************************

class Book {

public:
Book();
Book(const char* newTitle);
Book(char title);

int compareTo(const char* nextTitle);

char* getBook();


private:
char title[TITLE_LEN];
};

_________________________________________________

bookList.h

#pragma once
//********************************************************************
// BookList.h
//
// Represents a collection of books.
//*******************************************************************
#define LIST_LEN 1024
#include "Book.h"

class BookNode {
public:
   //--------------------------------------------------------------
   // Sets up the node
   //--------------------------------------------------------------
   BookNode() { };
   BookNode(Book* theBook) {
       book = theBook;
       next = NULL;
   };
   friend class BookList;

private:
   Book* book;
   BookNode* next;
};

class BookList {

   //----------------------------------------------------------------
   // Sets up an initially empty list of books.
   //----------------------------------------------------------------
public:
   void add(Book*);
   char* getBookList(char*, size_t);
   void insert(Book*);
   void delet(Book*);

   BookList() {
       head = NULL;
   };

private:
   BookNode* head;

};

_________________________________________________

pch.h

#pragma once
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

#ifndef PCH_H
#define PCH_H

// TODO: add headers that you want to pre-compile here

#endif //PCH_H

///////////////////////////////////////////////////////////////////////////////////////////////////

_________________________________________________

My .cpp Files

book.cpp

#include "BookList.h"
#include "Book.h"
#include
using namespace std;

Book::Book() {};

Book::Book(const char* newTitle, char* title)
{
strcpy_s(title, TITLE_LEN, newTitle);
}

int Book::compareTo(const char* nextTitle) {
return strcmp(title, nextTitle);
}

char* Book::getBook() {
return title;
}

_________________________________________________

bookList.cpp

//********************************************************************
// BookList.h
//
// Represents a collection of books.
//*******************************************************************
#include "pch.h"
#include "BookList.h"
#include "Book.h"
//----------------------------------------------------------------
// Creates a new Book object and adds it to the end of
// the linked list.
//----------------------------------------------------------------
void BookList::add(Book* newBook) {

   BookNode* node = new BookNode(newBook);
   BookNode* current;

   if (head == NULL)
       head = node;
   else {
       current = head;
       while (current->next != NULL)
           current = current->next;
       current->next = node;
   }
}

//----------------------------------------------------------------
// Returns this list of books as a string.
//----------------------------------------------------------------
char* BookList::getBookList(char* list, size_t list_len) {

   list[0] = '\0';
   BookNode* current = head;

   while (current != NULL) {
       strcat_s(list, list_len, current->book->getBook());
       strcat_s(list, list_len, "\n");
       current = current->next;
   }

   return list;
}

//----------------------------------------------------------------
// Deletes specified books from list.
//----------------------------------------------------------------
void BookList::delet(Book* newBook) {
if (head == NULL) {
return;
}

BookNode* node = new BookNode(newBook);

BookNode* current = NULL;
BookNode* previous;

if (head->book->compareTo(newBook->getBook()) == 0) {
current = head;
head = head->next;
delete current;
}
else {
previous = head;
current = head->next;

while (current && current->book->compareTo(newBook->getBook()) != 0) {
previous = current;
current = current->next;
}

if (current) {
previous->next = current->next;
delete current;
}
}
}

void BookList::insert(Book* newBook) {
BookNode* node = new BookNode(newBook);

if (head == NULL)
head = node;
else {

//current book < next book
if (newBook->compareTo(head->book->getBook()) < 0) {
node->next = head;
head = node;
}
else {
BookNode* current = head->next;
BookNode* prev = head;
while (current && newBook->compareTo(current->book->getBook()) > 0) {
prev = current;
current = current->next;
}

node->next = prev->next;
prev->next = node;
}
}
}

_________________________________________________

main.cpp

//lib.cpp
#include "Book.h"
#include "pch.h"
#include
#include "BookList.h"
using namespace std;

int main(int argc, char* argv[]) {
//--------------------------------------------------------------
// Creates a BookList object, adds several books to the list,
// then prints them.
//--------------------------------------------------------------

char list[LIST_LEN];
BookList* books = new BookList();

books->insert(new Book("F Title"));
books->insert(new Book("D Title"));
books->insert(new Book("G Title"));
books->insert(new Book("A Title"));
books->insert(new Book("E Title"));
books->insert(new Book("H Title"));

cout << "After inserts:\n";
cout << books->getBookList(list, TITLE_LEN) << endl;;

books->delet(new Book("A Title"));
books->delet(new Book("H Title"));
books->delet(new Book("G Title"));
books->delet(new Book("E Title"));

cout << "After deletes:\n";
cout << books->getBookList(list, TITLE_LEN) << endl;;

books->insert(new Book("A Title"));
books->insert(new Book("E Title"));
books->insert(new Book("H Title"));
books->insert(new Book("G Title"));

cout << "After 2nd inserts:\n";
cout << books->getBookList(list, TITLE_LEN) << endl;

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// pch.h
#pragma once
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

#ifndef PCH_H
#define PCH_H

// TODO: add headers that you want to pre-compile here

#endif //PCH_H

//end of pch.h

// Book.h

#pragma once
#include <iostream>
#include <cstring>
#define TITLE_LEN 81

using namespace std;

//********************************************************************
// Book.cpp
//
// Represents a single book.
//*******************************************************************

class Book {

public:
Book();
Book(const char* newTitle);
//Book(char title); // remove this constructor

int compareTo(const char* nextTitle);

char* getBook();


private:
char title[TITLE_LEN];
};

//end of Book.h

// Book.cpp
// No need to include BookList.h
#include "Book.h"
#include <cstring>
using namespace std;

Book::Book() {};
// correct the definition of constructor
Book::Book(const char* newTitle)
{
strcpy_s(title, TITLE_LEN, newTitle);
}

int Book::compareTo(const char* nextTitle) {
   return strcmp(title, nextTitle);
}

char* Book::getBook() {
   return title;
}

//end of Book.cpp

//Booklist.h

#pragma once
//********************************************************************
// BookList.h
//
// Represents a collection of books.
//*******************************************************************
#define LIST_LEN 1024
#include "Book.h"

class BookNode {
public:
//--------------------------------------------------------------
// Sets up the node
//--------------------------------------------------------------
BookNode() {book = NULL;next = NULL; };
BookNode(Book* theBook) {
book = theBook;
next = NULL;
};
friend class BookList;

private:
Book* book;
BookNode* next;
};

class BookList {

//----------------------------------------------------------------
// Sets up an initially empty list of books.
//----------------------------------------------------------------
public:
void add(Book*);
char* getBookList(char*, size_t);
void insert(Book*);
void delet(Book*);

BookList() {
head = NULL;
};
private:
BookNode* head;

};
//end of BookList.h

//********************************************************************
// BookList.cpp
//
// Represents a collection of books.
//*******************************************************************
#include "pch.h"
#include "BookList.h"
#include "Book.h"
//----------------------------------------------------------------
// Creates a new Book object and adds it to the end of
// the linked list.
//----------------------------------------------------------------
void BookList::add(Book* newBook) {

BookNode* node = new BookNode(newBook);
BookNode* current;

if (head == NULL)
head = node;
else {
current = head;
while (current->next != NULL)
current = current->next;
current->next = node;
}
}
//----------------------------------------------------------------
// Returns this list of books as a string.
//----------------------------------------------------------------
char* BookList::getBookList(char* list, size_t list_len) {

list[0] = '\0';
BookNode* current = head;

while (current != NULL) {
strcat_s(list, list_len, current->book->getBook());
strcat_s(list, list_len, "\n");
current = current->next;
}

return list;
}

//----------------------------------------------------------------
// Deletes specified books from list.
//----------------------------------------------------------------
void BookList::delet(Book* newBook) {
if (head == NULL) {
return;
}

// BookNode* node = new BookNode(newBook); node not used

BookNode* current = NULL;
BookNode* previous;

if (head->book->compareTo(newBook->getBook()) == 0) {
current = head;
head = head->next;
delete current;
}
else {
previous = head;
current = head->next;

while (current && current->book->compareTo(newBook->getBook()) != 0) {
previous = current;
current = current->next;
}

if (current) {
previous->next = current->next;
delete current;
}
}
}

// inserts newBook into the list

void BookList::insert(Book* newBook) {
BookNode* node = new BookNode(newBook);

if (head == NULL)
head = node;
else {

//current book < next book
if (newBook->compareTo(head->book->getBook()) < 0) {
node->next = head;
head = node;
}
else {
BookNode* current = head->next;
BookNode* prev = head;
while (current && newBook->compareTo(current->book->getBook()) > 0) {
prev = current;
current = current->next;
}

node->next = prev->next;
prev->next = node;
}
}
}

//end of BookList.cpp

//lib.cpp
#include "Book.h"
#include "pch.h"
#include <iostream>
#include "BookList.h"
using namespace std;

int main(int argc, char* argv[]) {
//--------------------------------------------------------------
// Creates a BookList object, adds several books to the list,
// then prints them.
//--------------------------------------------------------------

char list[LIST_LEN];
BookList* books = new BookList();

books->insert(new Book("F Title"));
books->insert(new Book("D Title"));
books->insert(new Book("G Title"));
books->insert(new Book("A Title"));
books->insert(new Book("E Title"));
books->insert(new Book("H Title"));

cout << "After inserts:\n";
cout << books->getBookList(list, TITLE_LEN) << endl;;

books->delet(new Book("A Title"));
books->delet(new Book("H Title"));
books->delet(new Book("G Title"));
books->delet(new Book("E Title"));

cout << "After deletes:\n";
cout << books->getBookList(list, TITLE_LEN) << endl;;

books->insert(new Book("A Title"));
books->insert(new Book("E Title"));
books->insert(new Book("H Title"));
books->insert(new Book("G Title"));

cout << "After 2nd inserts:\n";
cout << books->getBookList(list, TITLE_LEN) << endl;

return 0;
}

//end of lib.cpp

Output:

Add a comment
Know the answer?
Add Answer to:
Modify the booklist program (from the lecture notes) by adding delet and insert operations into the...
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
  • Hi I've a problem with this code. When I add more than 1 song to the...

    Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it.  Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...

  • 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...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: ...

    Hi! Can someone can convert this java code to c++. ASAP thanks I will thumbs up Here's the code: package lists; import bookdata.*; public class DoublyLinkedList { int size; //Variable que define el tamano de la lista. Node head, tail; //Nodos que definen el Head y Tail en la lista. //Constructor public DoublyLinkedList(){ this.head = null; this.tail = null; this.size = 0; } //Insert a new book in alphabetic order. public void insert(Book nb){ //Creamos uno nuevo nodo con el...

  • 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...

  • C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses...

    C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...

  • I need help and have to get this done asap: Using the following source codes provided below, create recursive functions...

    I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...

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