Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this homework, you wil implement a doubly linked list to maintain a list of b

ID: 3736016 • Letter: I

Question

In this homework, you wil implement a doubly linked list to maintain a list of books Requirements: Implement a doubly linked list in a file called doublyLinkedList.c . In a separate file, say bookList.c, first define using typedef a structure called Book containing the title, author, price data members. The author's type has to be a structure called Author containing firstname and lastname members. Price is a floating point number . Your program will start by reading a binary file that you get from argv[1] which contains a list of book records, and store them in a doubly linked list of book structures. Write a function called load to do this step. The file may or may not be ordered, but you have to maintain the linked list in alphabetical order of Author lastname . Your program then will display a menu for the user as follows 1 Add Book 2 Delete Book 3 Search by Author 4 Search by Title 5 Print Booklist 6 Save and Exit Based on user input, you will have to read more information from the user. For example, if the user wants to add a book, you need to read title, author firstname, author lastname and price from the user, and add that entry into your linked list in sorted alphabetical order of author lastname.

Explanation / Answer

Create a text file as below say as books.txt

-------------------------------------------------------

4

Java Database Connectivity
Bernard Van Haecke
IDG
2001
0-7645-3144-1
Database Programming with JDBC and Java
George Reese
O'Reilly
1997
1-56592-270-0
Java Threads
Scott Oaks, Henry Wong
O'Reilly
1999
1-56592-418-5
Java Swing
Robert Eckstein, Marc Loy & Dave Wood
O'Reilly
1998
1-56592-455-X

-----------------------------------------------------------------------------------------------

the below code actually create a list of menu to add, delete, search, get list and exist etc..

may you have to modifiy some of the names of functions but this what you are extacly looking for

Main.cpp

---------------------------------------------------------------------------------------------------------------------------------

//Krishnan Raghu, CIS 15C

//Data Structures with Mr. Nguyen

//NOTE: books.txt is automatically read as the program starts. Data is saved in saved.txt when the program ends.

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

struct book

{

string title;

string author;

string publisher;

int year;

string isbn;

book* next;

book(string, string, string, int, string, book*);

};

book::book(string tempTitle, string tempAuthor, string tempPublisher, int tempYear, string tempIsbn, book* tempNext)

:title(tempTitle), author(tempAuthor), publisher(tempPublisher), year(tempYear), isbn(tempIsbn), next(tempNext)

{}

typedef book* bookPtr;

void getline(istream &stream, string &str, char delimiter)

{ char temp[500];

stream.get(temp, 500, delimiter);

stream.ignore(500, delimiter);

str = temp;

}

void getline(istream &stream, int &num, char delimiter)

{ int temp;

stream >> temp;

stream.ignore(500, delimiter);

num= temp;

}

void readFile(bookPtr &root);

void insert (bookPtr &root);

void delTitle(bookPtr &root);

bookPtr locateNode(bookPtr temp, string tit);

void delIsbn(bookPtr &root);

bookPtr locateNodeIsbn(bookPtr temp, string isb);

void searchIsbn(bookPtr temp);

void printList(bookPtr temp);

void printAuthor(bookPtr temp);

void saveFile(bookPtr temp);

int countNodes(bookPtr temp);

int main()

{

int choice;

bookPtr root = NULL;

readFile(root);

do

{

cout << "Menu: Select your option ";

cout << "(1) Add a book to the list ";

cout << "(2) Delete a book based on Title ";

cout << "(3) Delete a book based on ISBN ";

cout << "(4) Search for a book by ISBN. ";

cout << "(5) List all books. ";

cout << "(6) List all books by an author. ";

cout << "(7) Quit. ";

cout << "Enter your choice ---> ";

cin >> choice;

if (1 <= choice && choice <= 6)

{

switch (choice)

{

case 1:

insert(root);

break;

case 2:

delTitle(root);

break;

case 3:

delIsbn(root);

break;

case 4:

searchIsbn(root);

break;

case 5:

printList(root);

break;

case 6:

printAuthor(root);

break;

default:

cout << "Invalid choice. Enter again. ";

break;

}

}

}

while (choice != 7);

saveFile(root);

return 0;

}

void readFile(bookPtr &root)

{

int numBooks, yea;

string tit, aut, pub, isb;

ifstream infile ("books.txt", ios::in);

infile >> numBooks;

infile.ignore(500,' ');

for (int count = 0; count < numBooks; count++)

{

getline(infile, tit, ' ');

getline(infile, aut, ' ');

getline(infile,pub, ' ');

getline(infile,yea, ' ');

getline(infile, isb, ' ');

root = new book (tit, aut, pub, yea, isb, root);

}

}

void insert (bookPtr &root)

{

string tit, aut, pub, isb;

int yea;

cout << "Title: ";

cin.ignore(500,' ');

getline(cin, tit, ' ');

cout << "Author: ";

getline(cin, aut, ' ');

cout << "Publisher: ";

getline(cin,pub, ' ');

cout << "Year: ";

getline(cin,yea, ' ');

cout << "ISBN: ";

getline(cin, isb, ' ');

root = new book (tit, aut, pub, yea, isb, root);

}

void delTitle(bookPtr &root)

{

string tit;

cout << "Book Title: ";

cin.ignore(500,' ');

getline(cin, tit, ' ');

bookPtr p = locateNode(root, tit);

if (p == NULL)

cout << " Deletion cannot be done. ";

else if (root == p)

root = p->next;

else

{

bookPtr q = root;

while (q->next != p)

q = q->next;

q->next = p->next;

}

delete p;

}

bookPtr locateNode(bookPtr temp, string tit)

{

while (temp != NULL)

{

if (temp->title == tit)

{

return temp;

}

temp = temp->next;

}

return NULL;

}

void delIsbn(bookPtr &root)

{

string isb;

cout << "Book ISBN: ";

cin.ignore(500,' ');

getline(cin, isb, ' ');

bookPtr p = locateNodeIsbn(root, isb);

if (p == NULL)

cout << " Deletion cannot be done. ";

else if (root == p)

root = p->next;

else

{

bookPtr q = root;

while (q->next != p)

q = q->next;

q->next = p->next;

}

delete p;

}

bookPtr locateNodeIsbn(bookPtr temp, string isb)

{

while (temp != NULL)

{

if (temp->isbn == isb)

{

return temp;

}

temp = temp->next;

}

return NULL;

}

void searchIsbn(bookPtr temp)

{

string isb;

cout << "Book ISBN: ";

cin.ignore(500,' ');

getline(cin, isb, ' ');

while (temp != NULL)

{

if (isb == temp->isbn)

{

cout << temp->title << " ";

cout << temp->author << " ";

cout << temp->publisher << " ";

cout << temp->year << " ";

cout << temp->isbn << " ";

}

temp = temp->next;

}

cout << " ";

}

void printList(bookPtr temp)

{

while (temp != NULL)

{

cout << temp->title << " ";

cout << temp->author << " ";

cout << temp->publisher << " ";

cout << temp->year << " ";

cout << temp->isbn << " ";

temp = temp->next;

}

cout << " ";

}

void printAuthor(bookPtr temp)

{

string aut;

cout << "Author name: ";

cin.ignore(500,' ');

getline(cin, aut, ' ');

while (temp != NULL)

{

if (temp->author == aut)

{

cout << temp->title << " ";

cout << temp->author << " ";

cout << temp->publisher << " ";

cout << temp->year << " ";

cout << temp->isbn << " ";

}

temp = temp->next;

}

cout << " ";

}

void saveFile(bookPtr temp)

{

int count = countNodes(temp);

ofstream outFile("saved.txt",ios::out);

outFile << count << " ";

while (temp != NULL)

{

outFile << temp->title << " ";

outFile << temp->author << " ";

outFile << temp->publisher << " ";

outFile << temp->year << " ";

outFile << temp->isbn << " ";

temp = temp->next;

}

cout << " ";

}

int countNodes(bookPtr temp)

{

int countB = 0;

while (temp != NULL)

{

countB++;

temp = temp->next;

}

return countB;

}

---------------------------------------------------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote