Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Client program Your program will ask the user for the file name, open the text file , read each category / line record and store
Client program
Your program will ask the user for the file name, open the text file read each categoryline record and store into two strings. Make sure the file opened correctly before you start processing it You do not know how many records are in the file so your program should read until it gets to the end of the file.
Your program will invoke the add method on the linked list object passing the category and line of text for the current record.
It will then display the contents of the list the records that were read from the file Display the category once with all of the lines in that category indented.
You will use a class method toVector that puts all of the categoryline pairs into a vector and returns it to the program that will display it
Add Method Details
Your add method will create the node, set the values and put the node in the list at the correct location. The lines of text should be grouped by category and in ascending order within each category. Put the categories into the linked list in the same order they were first encountered in the file. The program will continue to do this until the end of the file has been reached.
Add a node this method will take as input two string values the category and the line. It will create a node object and set the attributes. Then it will put the node in the linked list in alphabetical order grouped by category. The categories will not be in sorted order they will be in the order they were encountered in the file, but the lines within each category will be in alphabetical order. You will not add the items then sort them later you MUST put them in the correct spot one time. You will not use a vector for any of this process you are to only process your linked list.
toVector returns vector with the contents of the list which will be a string category followed by a string line of text from the file. You will use only the pushback method to get the strings into the vector.
Destructor
#include
#include
#include
#include
using namespace std;
Node class for the linked list
class Node
public:
std::string category;
std::string line;
Node next;
Nodeconst string& data : linedata nextnullptr
;
Linked list class
class LinkedList
private:
Node headPtr;
int numItems;
public:
LinkedList : headPtrnullptr numItems
~LinkedList;
void addNodeconst string& line;
void displayList;
;
LinkedList::~LinkedList
while headPtr nullptr
Node temp headPtr;
headPtr headPtrnext;
delete temp;
void LinkedList::addNodeconst string& line
Node newNode new Nodeline;
if headPtr nullptr
headPtr newNode;
else
Node curPtr headPtr;
while curPtrnext nullptr
curPtr curPtrnext;
curPtrnext newNode;
void LinkedList::displayList
Node curPtr headPtr;
while curPtr nullptr
cout curPtrline endl;
curPtr curPtrnext;
int main
LinkedList categories; Assuming categories: Boring, Interesting, Name, Nonsense, Others
string filename Usersinputtxt; Change this to your input file name
ifstream inputFilefilename;
if inputFile
cerr "Error opening file filename endl;
return ;
string line;
string category;
while getlineinputFile line
if lineempty continue;
Check the category and add line to the respective linked list
if category "Boring" categoriesaddNodeline;
else if category "Interesting" categoriesaddNodeline;
else if category "Name" categoriesaddNodeline;
else if category "Nonsense" categoriesaddNodeline;
else categoriesaddNodeline; Others
inputFile.close;
Display the lines in the specified order
for int i ; i ; i
categoriesidisplayList;
return ;
Boring
The sky is blue.
Name
Clovis Bagwell
Interesting
The quick brown fox jumped over the lazy dog.
Interesting
Shake rattle and roll.
Nonsense
One red bean stuck in the bottom of a tin bowl.
Interesting
Rats live on no evil star.
Name
Mark Knopfler
Nonsense
Ceramic fists artificial deceased cists rancid buds burst
Interesting
It is impossible for most people to lick their own elbow.
Interesting
An ostrich's eye is bigger than its brain.
Name
Archibald Beechcroft
Boring
Workers of the world unite.
Nonsense
A squid eating dough in a polyethylene bag is fast and bulbous.
Name
Oliver Crangle
Interesting
Rubber bands last longer when refrigerated.
Boring
Data structures is really hard.
Boring
It's rain
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started