This program is going to define a preparatory class that you will be using for l
ID: 3890924 • 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
Reminder:
Be sure that your program includes your name and ID and the necessary comments, and follows the coding standards as listed in the document posted on the Blackboard.
Carefully test your program.
You are welcome to write your program at home. If you do, be sure to compile and test it in the lab before submitting it.
Be sure and name your program properly or you will get a grade of 0 on the assignment.
Be sure to turn in 4 separate files – node.cpp, node.hpp, main.cpp, and makefile.
Your file names will be makefile, main.cpp, node.cpp, and node.hpp. Name the executable book.
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
struct Book {
string title;
string author;
string dateread;
};
class Node {
private:
Book data;
Node *ptr;
public:
Node(){
ptr = NULL;
}
Node(string t, string a, string d){
data.title = t;
data.author = a;
data.dateread = d;
}
Node *getPtr(){
return ptr;
}
Node *setPtr(Node *p){
ptr = p;
}
Book getBook(){
return data;
}
bool compare_data(Node *a){
if (data.title == a->getBook().title){
return true;
}
else {
return false;
}
}
void process_data(){
cout << data.title << " " << data.author << " " << data.dateread << endl;
}
};
int main(){
Node *node1 = new Node("title1","author1","date1");
Node *node2 = new Node("title2","author2","date2");
Node *node3 = new Node("title3","author3","date3");
Node *node4 = new Node("title4","author4","date4");
Node *node5 = new Node("title5","author5","date5");
Node *head;
head = node1;
node1->setPtr(node2);
node2->setPtr(node3);
node3->setPtr(node4);
node4->setPtr(node5);
node5->setPtr(NULL);
while (head != NULL){
head->process_data();
head = head->getPtr();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.