In this programming assignment, you are about to implement the library you did i
ID: 3550093 • Letter: I
Question
In this programming assignment, you are about to implement the library you did in the previous programming assignment using LINKED LIST method. All the requirements are the same as those in the previous one.
I have this previous assignment code and now i need to write the same code using LINKED LIST
#include "Library.h"
int main()
{
Library lib;
int quitFlag=1;
do
{
char choice;
cout<<" Welcome to the Library ";
cout<<"Please select one option ";
cout<<" S (Search) I (Insert) D (Delete) P (Print) Q (Quit) ";
cout<<"Enter Your Choice Number: ";
cin>>choice;
switch(choice)
{
case 'S':
case 's':
lib.search();
break;
case 'I':
case 'i':
lib.insertion();
break;
case 'D':
case 'd':
lib.deletion();
break;
case 'P':
case 'p':
lib.print();
break;
case 'Q':
case 'q':
quitFlag = lib.quit();
break;
}
}while(quitFlag);
return 0;
}
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
#include
#include
#define MAX 100
using namespace std;
class Book
{
public:
int getISBN() { return ISBN; };
void setISBN(int isbn) { ISBN = isbn; }
private:
int ISBN;
};
class Library
{
private:
Book books[100];
int numOfBooks;
public:
Library();
~Library();
void search();
void insertion();
void deletion();
void print();
int quit();
};
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
#include "Library.h"
Library::Library()
{
numOfBooks = 0;
}
Library::~Library()
{
}
void Library::search()
{
int localIsbn;
cout<<"Enter Books ISBN : ";
cin>>localIsbn;
int i=0;
for(i=0;i>localIsbn;
if(numOfBooks==100)
{
cout<<"there is no more room for new books ";
}
else
{
Book temp;
temp.setISBN(localIsbn);
books[numOfBooks++]=temp;
cout<<"Book added in the system succesfully ";
}
}
//________________________
void Library::deletion()
{
int localIsbn;
cout<<"Enter Books ISBN : ";
cin>>localIsbn;
if(numOfBooks==0)
{
cout<<"There are no books in the system ";
}
else
{
int i;
for(i=0;i>choice;
return choice;
}
Explanation / Answer
Link to the library code using linked list - https://www.dropbox.com/sh/36bd0g5dka7mgie/BqloCI70fk
Compiling the code - "g++ Library.cpp main.cpp"
Running the code - "./a.out"
Library.h
#include<iostream>
#include<iomanip>
#include <list>
#define MAX 100
using namespace std;
class Book
{
public:
Book(int a) : ISBN(a)
{}
Book()
{}
int getISBN() { return ISBN; };
void setISBN(int isbn) { ISBN = isbn; }
private:
int ISBN;
};
class Library
{
private:
list<Book> books;
public:
Library();
~Library();
void search();
void insertion();
void deletion();
void print();
int quit();
};
Library.cpp
#include "Library.h"
Library::Library()
{
}
Library::~Library()
{
}
void Library::search()
{
int localIsbn;
cout<<"Enter Books ISBN : ";
cin>>localIsbn;
int i=0;
list<Book>::iterator it;
for (std::list<Book>::iterator it=books.begin(); it != books.end(); ++it)
{
if((*it).getISBN()==localIsbn)
{
cout<<"Book has been found ";
cout << "Book ISBN: " << (*it).getISBN()<<endl;
break;
}
i++;
}
if(i==books.size())
{
cout<<"This book is not in the system ";
}
}
//_________________________________________________________________________
void Library::insertion()
{
int localIsbn;
cout<<"Enter Books ISBN : ";
cin>>localIsbn;
Book temp;
temp.setISBN(localIsbn);
books.push_back(temp);
cout<<"Book added in the system succesfully ";
}
//_________________________________________________________________________
void Library::deletion()
{
int localIsbn;
cout<<"Enter Books ISBN : ";
cin>>localIsbn;
Book temp;
temp.setISBN(localIsbn);
if(books.size()==0)
{
cout<<"There are no books in the system ";
}
else
{
int i=0;
list<Book>::iterator it;
for (std::list<Book>::iterator it=books.begin(); it != books.end(); ++it)
{
if((*it).getISBN()==localIsbn)
{
books.erase(it);
cout<<"Book has been deleted ";
break;
}
i++;
}
if(i==books.size())
{
cout<<"This book is not availabe ";
}
}
}
//_________________________________________________________________________
void Library::print()
{
//print all books info
if (books.size()==0)
{
cout<<"There are no books in the system right now. ";
}
else
{
cout<<"List of available Books are: "<<setw(10)<<"ISBN"<<endl;
list<Book>::iterator it;
for (std::list<Book>::iterator it=books.begin(); it != books.end(); ++it)
{
cout << setw(10) << (*it).getISBN() << endl;
}
cout<<"Total "<<books.size()<<"books"<<endl;
}
}
//_________________________________________________________________________
int Library::quit()
{
int choice;
cout<<"Enter 0 to Quit ";
cin>>choice;
return choice;
}
main.cpp
#include "Library.h"
int main()
{
Library lib;
int quitFlag=1;
do
{
char choice;
cout<<" Welcome to the Library ";
cout<<"Please select one option ";
cout<<" S (Search) I (Insert) D (Delete) P (Print) Q (Quit) ";
cout<<"Enter Your Choice Number: ";
cin>>choice;
switch(choice)
{
case 'S':
case 's':
lib.search();
break;
case 'I':
case 'i':
lib.insertion();
break;
case 'D':
case 'd':
lib.deletion();
break;
case 'P':
case 'p':
lib.print();
break;
case 'Q':
case 'q':
quitFlag = lib.quit();
break;
}
}while(quitFlag);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.