This program is going to define a preparatory class that you will be using for l
ID: 3887983 • Letter: T
Question
This program is going to define a preparatory class that you will be using for linked list. You are going to write this class and use it so that you will be ready to use it for the linked list class. The class is going to be the Node class.
Structure
• Book title – must hold the title of a book including spaces.
• Author – must hold the author’s name including spaces
• Date read – must be able to contain numerical mo, day, year (digits)
Node class data:
• declaration of the structure class
• Pointer to class Node
Node class member functions
• Default constructor
• Constructor that takes book title, author, and date
• Mutator function to set pointer
• Accessor function to return pointer
• Accessor function to return structure
• compare_data function that will compare a given book title against the one in the class
• process_data function that will print the data to the screen.
Specific information for class:
Define a class called Node. The Node class will hold a declaration of the structure and a pointer to a Node.
The default constructor will set the book title to your favorite book, author to the author of the book, and the date read to some default date that you chose. The pointer will be set to NULL.
There will another constructor that will accept the book title, author, and the date. The pointer will be set to NULL.
Define a mutator function that will allow the pointer to be changed.
Write an accessor function that will allow the pointer to be returned.
Write another accessor function that will return the structure.
The class will have a compare_data function that will compare the title of a book passed to the function against the book in the class. It will return true if the title is the same and false otherwise.
Write a member function called process_data that will print the data to the screen.
Requirements for main:
In the main program, declare 5 pointers for the Node class.
Create dynamic memory for 5 different books and initialize them to book titles and dates that you choose. You will now have 5 Nodes with book titles and dates and each of the pointers set to NULL.
Create a Node pointer called head. Next, you are going to create a linked list with the 5 pointers. You are going to do this by setting up the head pointer to point to the first book. Each of the rest of the books will be linked together with the last book pointer being equal to NULL. Do this by setting the pointer of the first book (Node *) equal to the pointer of the second book. Set the pointer of the second book equal to the pointer of the third book. Set the pointer of the third book to the pointer of the fourth book and the pointer of the fourth book to the pointer of the fifth book. The fifth book pointer should remain set to NULL. Do not ask the user for input. Generate all of the data in your program.
For each object that you created, test each function in the class. (You should already have used the pointer mutator.)
Using a loop starting with the head pointer, traverse the list and execute the process_data in the Node class.
There will be no user input.
Sample output:
The Great Divorce C. S. Lewis 2/17/2016
Explanation / Answer
Given below is the implementation of the Node class and the main program as specified in the question. The output is also shown.
Hope the answer helped. If it did, please do rate it. Thank you very much.
compilation: g++ Node.cpp Main.cpp
run: ./a.out
Node.h
#ifndef Node_h
#define Node_h
#include <iostream>
using std::string;
struct Date
{
int month, day, year; //details of the date
};
struct Book
{
string title;
string author;
Date dateRead;
};
class Node
{
private:
struct Book book; //the book data inside this node
Node* next; //pointer to next node
public:
Node(); //default constructor
Node(string title, string author, Date dateRead);// parametrized constructor
void setNext(Node *nxt);
Node* getNext();
Book getBook();
bool compare_data(string title); //return true if the book title matched parameter, otherwise false
void process_data(); //print the book details
};
#endif /* Node_h */
Node.cpp
#include "Node.h"
using namespace std;
Node::Node() //default constructor
{
//set to your favorite book details
book.title = "Harry Potter and the Goblet of Fire";
book.author = "J. K. Rowling";
Date dt;
dt.month = 1;
dt.day = 1;
dt.year = 2017;
book.dateRead = dt;
next = NULL;
}
Node::Node(string title, string author, Date dateRead)// parametrized constructor
{
book.title = title;
book.author = author;
book.dateRead = dateRead;
next = NULL;
}
void Node::setNext(Node *nxt)
{
this->next = nxt;
}
Node* Node::getNext()
{
return next;
}
Book Node::getBook()
{
return book;
}
//compare the title with this node's book data.
//return true if the book title matched parameter, otherwise false.
bool Node::compare_data(string title)
{
return title == book.title;
}
//print the book details
void Node::process_data()
{
cout << "Title: " << book.title << endl;
cout << "Author: " << book.author << endl;
cout << "Date Read: " << book.dateRead.month << "/" << book.dateRead.day << "/" << book.dateRead.year << endl;
cout << endl;
}
Main.cpp
#include <iostream>
#include "Node.h"
using namespace std;
int main()
{
Date dt;
dt.month = 1;
dt.day = 5;
dt.year = 2017;
//create 5 pointers of Nodes with different boooks
Node* book1 = new Node("Great Stories for Children", "Ruskin Bond", dt);
dt.day = 10;
Node* book2 = new Node("The Blue Umbrella", "Ruskin Bond", dt);
dt.month = 4;
dt.day = 15;
Node* book3 = new Node("The Boy in the Striped Pyjamas", "John Boyne", dt);
dt.month = 5;
dt.day = 20;
Node* book4 = new Node("Diary of a Wimpy Kid", "Jeff Kinney", dt);
dt.month = 7;
dt.day = 9;
Node* book5 = new Node("The Adventures of Tom Sawyer", "Mark Twain", dt);
//now create the linked list
Node* head = book1;
//link the books
book1->setNext(book2);
book2->setNext(book3);
book3->setNext(book4);
book4->setNext(book5);
string title = "The Blue Umbrella";
cout << "title = " << title << endl <<endl;
cout << "book1.title = " << book1->getBook().title << endl;
cout << "book1.compare_data(title) = " << book1->compare_data(title) << endl << endl;
cout << "book2.title = " << book2->getBook().title << endl;
cout << "book2.compare_data(title) = " << book2->compare_data(title) << endl << endl;
cout << "Display the linked list - " << endl;
Node* current = head;
while(current != NULL)
{
current->process_data();
current = current->getNext();
}
}
output
title = The Blue Umbrella
book1.title = Great Stories for Children
book1.compare_data(title) = 0
book2.title = The Blue Umbrella
book2.compare_data(title) = 1
Display the linked list -
Title: Great Stories for Children
Author: Ruskin Bond
Date Read: 1/5/2017
Title: The Blue Umbrella
Author: Ruskin Bond
Date Read: 1/10/2017
Title: The Boy in the Striped Pyjamas
Author: John Boyne
Date Read: 4/15/2017
Title: Diary of a Wimpy Kid
Author: Jeff Kinney
Date Read: 5/20/2017
Title: The Adventures of Tom Sawyer
Author: Mark Twain
Date Read: 7/9/2017
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.