Hello. I am trying to write a doubly linked list that uses data from a class. I have this class that I would like to use...
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
int PubYear; //Variables
string Title, Author, Genre, Publisher;
public:
Book( ){}; //Default Constructor
void setTitle( string BookTitle )
{ Title = BookTitle; }
void setAuthor( string AuthorName)
{ Author = AuthorName; }
void setGenre( string GenreType )
{ Genre = GenreType; }
void setPublisher( string Pub )
{ Publisher = Pub; }
void setPubYear( int Year )
{ PubYear = Year; }
int GetYear() //Accessors
{return PubYear;}
string GetTitle()
{return Title;}
string GetAuthor()
{return Author;}
string GetGenre()
{return Genre;}
string GetPublisher()
{return Publisher;}
void setBook( string BookTitle, string AuthorName, string GenreType, string Pub, int Year )
{
setTitle( BookTitle ); //Mutators
setAuthor( AuthorName );
setGenre( GenreType );
setPublisher( Pub );
setPubYear( Year );
}
void print() //Operation that prints the book.
{
cout << "Title: " << GetTitle() << endl
<< "Author: " << GetAuthor() << endl
<< "Genre: " << GetGenre() << endl
<< "Publisher: " << GetPublisher() << endl
<< "Year Published: " << GetYear() << endl;
};
void Age() //Operation that determines the age of the book.
{
cout << GetTitle() << " is " << 2009 - GetYear() << " years old." << endl;
};
void CompareAuthor()
{
cout << "Compare authors of book1 and book2" << endl;
if(GetAuthor() == GetAuthor())
cout << "True" << endl;
else
cout << "False" << endl;
};
void CompareGenre()
{
cout << "Is " << GetTitle() << " non-fiction? ";
if(GetGenre() == "Non-Fiction")
cout << "True" << endl;
else
cout << "False" << endl;
};
};
//-------------------------------------------------
int main()
{
Book book1, book2, book3, book4, book5;
//Declaring my Books
cout << "Start of test:\n\n";
book1.setBook("In Cold Blood", "Truman Capote", "Non-Fiction", "Random House", 1966);
book1.print(); cout << endl;
book2.setBook("On the Road", "Jack Kerouac", "Fiction", "Peguin Books", 1955);
book2.print(); cout << endl;
book3.setBook("Slapstick", "Kurt Vonnegut", "Fiction", "Delacorte Press", 1976);
book3.print(); cout << endl;
book4.setBook("A Clockwork Orange", "Anthony Burgess", "Fiction", "Ballantine Books", 1962);
book4.print(); cout << endl;
l
book5.setBook("Slaughterhouse-five", "Kurt Vonnegut", "Fiction", "Dell Publishing", 1966);
book5.print(); cout << endl;
book1.Age(); book3.Age();//Printing the age of book1 and book3.
book3.CompareAuthor(); book1.CompareAuthor(); book2.CompareAuthor(); book4.CompareAuthor();
book1.CompareGenre(); book2.CompareGenre();
}
I need to use the data from this class (the books) and put them into a doubly linked list. I really at a loss as to how to do this. Here is what I've started...
#include <iostream>
#include <string>
using namespace std;
//-----------------------------------------------------------------------------
class Book {
private:
int PubYear; //Variables
string Title, Author, Genre, Publisher;
public:
Book( ){}; //Default Constructor
Book(string Booktitle, string AuthorName, string GenreType, string Pub, int year);
Book(const Book& b);
void setTitle( string BookTitle )
{ Title = BookTitle; }
void setAuthor( string AuthorName)
{ Author = AuthorName; }
void setGenre( string GenreType )
{ Genre = GenreType; }
void setPublisher( string Pub )
{ Publisher = Pub; }
void setPubYear( int Year )
{ PubYear = Year; }
int GetYear() //Accessors
{return PubYear;}
string GetTitle()
{return Title;}
string GetAuthor()
{return Author;}
string GetGenre()
{return Genre;}
string GetPublisher()
{return Publisher;}
void setBook( string BookTitle, string AuthorName, string GenreType, string Pub, int Year )
{
setTitle( BookTitle ); //Mutators
setAuthor( AuthorName );
setGenre( GenreType );
setPublisher( Pub );
setPubYear( Year );
}
void print() //Operation that prints the book.
{
cout << "Title: " << GetTitle() << endl
<< "Author: " << GetAuthor() << endl
<< "Genre: " << GetGenre() << endl
<< "Publisher: " << GetPublisher() << endl
<< "Year Published: " << GetYear() << endl;
};
friend istream& operator>>(istream& is, Book& b);
friend ostream& operator<<(ostream& os, Book& b);
};
//--------------------------------------------------------------------------------------
class Node {
string data;
Node *next;
public:
Node() : data(0), next(0) { }
Node(string e) : data(e), next(0) { }
Node(string e, Node* n) : data(e), next(n) { }
Node *getNext(void) const { return next; }
string getData(void) const { return data; }
void setData(string e) { data = e; }
void setNext(Node* n) { next = n; }
};
class List {
Node *start, book1;
public:
List();
void push_front(string);
void push_back(Book b);
Node *find(Book&);
void insert(Node *, Book&);
string pop_front();
void pop_back();
friend ostream& operator<<(ostream&, const List&);
};
//----------------------------------------------------------------------------
string List::pop_front(){
string answer;
if(start!=NULL){
answer = start->getData();
Node *save = start;
start=start->getNext();
delete save;}
else answer = "void"; // or any other sentinel value
return answer;
}
void List::pop_back(){
string answer;
if(start!=NULL){
Node *t1 = start,*t2;
while(t1->getNext()!=0){
t2=t1;
t1=t1->getNext();}
answer=t1->getData();
t2->setNext(0);
delete t1;}
else answer = "void"; // or any other sentinel value
return answer;
}
Node *List::find(Book&) {
Node *temp = start;
while (temp) {
if (temp->getData() == Book) return temp;
temp = temp->getNext();
}
return 0;
}
void List::insert(Node *ptr, Book& b) {
Node *newone = new Node(Book, ptr->getNext());
ptr->setNext(newone);
return;
}
ostream& operator<<(ostream& os, const List& l) {
os << "< ";
Node *temp = l.start;
while (temp) {
os << temp->getData() << " ";
temp = temp->getNext();
}
os << ">";
return os;
}
List::List() : start(0) { }
void List::push_front(string e) {
Node *newone = new Node(e,start);
start = newone;
return;
}
void List::push_back(Book b) {
Node *newone = new Node(b);
if (start == NULL)
start = newone;
else {
Node *temp = start;
while (temp->getNext() != NULL)
temp = temp->getNext();
temp->setNext(newone);
}
return;
}
int main( ) {
Book book1, book2, book3, book4, book5;
//Book book1, book2, book3, book4, book5;
//Declaring my Books
cout << "Start of test:\n\n";
book1.setBook("In Cold Blood", "Truman Capote", "Non-Fiction", "Random House", 1966);
book1.print(); cout << endl;
book2.setBook("On the Road", "Jack Kerouac", "Fiction", "Peguin Books", 1955);
book2.print(); cout << endl;
book3.setBook("Slapstick", "Kurt Vonnegut", "Fiction", "Delacorte Press", 1976);
book3.print(); cout << endl;
book4.setBook("A Clockwork Orange", "Anthony Burgess", "Fiction", "Ballantine Books", 1962);
book4.print(); cout << endl;
book5.setBook("Slaughterhouse-five", "Kurt Vonnegut", "Fiction", "Dell Publishing", 1966);
book5.print(); cout << endl;
List a;
a.push_back(book1);
//a.push_back("b");
//a.push_back("c");
//a.push_back("d");
//a.push_back("e");
cout << a << endl;
Node *temp;
temp = a.find(book1);
a.insert(temp, book2);
//temp = a.find("b");
//a.insert(temp, "g");
//temp = a.find("c");
//a.insert(temp, "h");
cout << a << endl;
int x;
cin >> x;
return 0;
I need to have those three classes (node, list, and book) in my program. How can I put my book class into my list? And how can I make use of pop back, push front, insert, etc.? Any help you can provide would be great... Thanks.