1. Write a program in the file main.cpp to read in words from a file with the na
ID: 3589259 • Letter: 1
Question
1. Write a program in the file main.cpp to read in words from a file with the name input.txt, store the words in a linked list and count how many times each word appears. Use a linked list of nodes defined by the wordNode class in the attached file main.cpp
The words in the list should be kept in alphabetical order. That is, as the words are read from the input file they should be inserted in the list so that the list always remains in alphabetical order. The program should read the words from a file with the name input.txt. Create the linked list of wordNodes based on the file data. Add a function to print out the words along with each words count on separate lines in alphabetical order.
2. Use the bubble sort function provided in main.cpp program file. This function rearranges the nodes in the linked list so that the nodes are ordered by word count from highest to lowest.
You will need to write the swapCount( ) function that the orderByCount( ) function calls. Also add code to the main program, after the code you added for step one, so that it calls the orderByCount( ) function and then calls a function to print out the words in the linked-list in count order. You will need to write the function that prints out the list in the count order.
File to work with:
Thank you!
Explanation / Answer
Given below is the completed code for the question. Hope it helps. Please don't forget to rate the answer if it helped. Thank you.
//
// linked List Main Program
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
class wordNode {
public:
wordNode(string word, int count, wordNode* nextNodePtr);
void InsertAfter(wordNode* nodePtr);
wordNode* GetNextPtr();
int GetCount();
string GetWord();
void SetWord(string inWord);
void SetCount(int inCount);
private:
string word;
int count;
wordNode* nextNodePtr;
};
wordNode::wordNode(string inWord, int inCount, wordNode* inNextNodePtr = nullptr) {
word = inWord;
count = inCount;
nextNodePtr = inNextNodePtr;
};
void wordNode::InsertAfter(wordNode* nodePtr)
{
if(nodePtr == nullptr) return;
this->nextNodePtr = nodePtr->nextNodePtr;
nodePtr->nextNodePtr = this;
}
wordNode* wordNode::GetNextPtr()
{
return nextNodePtr;
}
string wordNode::GetWord()
{
return word;
}
int wordNode::GetCount()
{
return count;
}
void wordNode::SetWord(string inWord)
{
word = inWord;
}
void wordNode::SetCount(int inCount)
{
count = inCount;
}
//
// swap the contents of two wordNode objects
//
void swapWordNodes(wordNode* wordNode1, wordNode* wordNode2){
string tempStr = wordNode1->GetWord();
wordNode1->SetWord(wordNode2->GetWord());
wordNode2->SetWord(tempStr);
int tempCount = wordNode1->GetCount();
wordNode1->SetCount(wordNode2->GetCount());
wordNode2->SetCount(tempCount);
}
wordNode* getLastNodeAddr(wordNode* first)
{
if(first == nullptr) return nullptr;
while(first->GetNextPtr() != nullptr)
{
first = first->GetNextPtr();
}
return first;
}
//
// Bubble Sort by wordNode count
//
void orderByCount(wordNode* firstNode){
wordNode* lastNode = getLastNodeAddr(firstNode);
wordNode* currentNode = nullptr;
wordNode* nextNode = nullptr;
wordNode* newLastNode = nullptr;
while (firstNode != lastNode){
currentNode = firstNode;
while (currentNode != lastNode){
nextNode = currentNode->GetNextPtr();
if (currentNode->GetCount() < nextNode->GetCount())
swapWordNodes(currentNode, nextNode);
newLastNode = currentNode;
currentNode = currentNode->GetNextPtr();
};
lastNode = newLastNode;
};
}
//
// Print out words in the linked list in alphabetical orde along with the word count.
//
void printAlphaOrder(wordNode* list){
cout << "The alphabetical order of the list is " << endl;
wordNode* curr = list;
while(curr != NULL)
{
cout << curr->GetWord() << "(" << curr->GetCount() << ")" << endl;
curr = curr->GetNextPtr();
}
cout << "======================================= " << endl << endl;
}
//
// Print out words in the linked-list in count order from highest to lowest count
//
void printCountOrder(wordNode* list){
orderByCount(list);
wordNode* curr = list;
cout << "The list sorted on word count is " << endl;
while(curr != NULL)
{
cout << curr->GetWord() << "(" << curr->GetCount() << ")" << endl;
curr = curr->GetNextPtr();
}
cout << "======================================= " << endl << endl;
}
//
// Read in words from a file and add them to the linked-list while
// keeping track of the number of times each word in the file appears.
//
int main(){
wordNode *list = NULL;
ifstream ifs("/Users/raji/Documents/Chegg/c++/Test2/Test2/input.txt");
if(!ifs.is_open())
{
cout << "Error opening input.txt" << endl;
return 1;
}
//
// add words to linked-list from an input file with the name "input.txt"
// and print out words in linked list along with their associated word count
//
string word;
while(ifs >> word)
{
//cout << word << endl;
if(list == NULL)
list = new wordNode(word, 1, NULL);
else
{
if(word < list->GetWord()) //insert before 1st node
{
list = new wordNode(word, 1, list);
}
else
{
wordNode* prev = list;
wordNode* curr = list;
bool inserted = false;
while(curr != NULL)
{
if(curr->GetWord() == word) //word already in list, increment count
{
curr->SetCount(curr->GetCount() + 1);
inserted = true;
break;
}
else if(curr->GetWord() > word) //new word, insert a node with count 1
{
wordNode* n = new wordNode(word, 1, NULL);
n->InsertAfter(prev);
inserted = true;
break;
}
prev = curr;
curr = curr->GetNextPtr();
}
if(!inserted)
{
wordNode* n = new wordNode(word, 1, NULL);
n->InsertAfter(prev);
}
}
}
}
ifs.close();
printAlphaOrder(list);
//
// reorder words in linked-list by the decreasing value of the wordNode counts and
// print out the words in word count order from highest to lowest
//
printCountOrder(list);
}
input file: input.txt
The unanimous Declaration of the thirteen united States of America When in the Course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.
output
The alphabetical order of the list is
America(1)
Course(1)
Declaration(1)
God(1)
Laws(1)
Nature(1)
Nature's(1)
States(1)
The(1)
When(1)
a(1)
among(1)
and(3)
another(1)
assume(1)
bands(1)
becomes(1)
causes(1)
connected(1)
decent(1)
declare(1)
dissolve(1)
earth,(1)
entitle(1)
equal(1)
events(1)
for(1)
have(1)
human(1)
impel(1)
in(1)
it(1)
mankind(1)
necessary(1)
of(7)
one(1)
opinions(1)
people(1)
political(1)
powers(1)
requires(1)
respect(1)
separate(1)
separation.(1)
should(1)
station(1)
that(1)
the(10)
them(2)
them,(1)
they(1)
thirteen(1)
to(5)
unanimous(1)
united(1)
which(3)
with(1)
=======================================
The list sorted on word count is
the(10)
of(7)
to(5)
and(3)
which(3)
them(2)
America(1)
Course(1)
Declaration(1)
God(1)
Laws(1)
Nature(1)
Nature's(1)
States(1)
The(1)
When(1)
a(1)
among(1)
another(1)
assume(1)
bands(1)
becomes(1)
causes(1)
connected(1)
decent(1)
declare(1)
dissolve(1)
earth,(1)
entitle(1)
equal(1)
events(1)
for(1)
have(1)
human(1)
impel(1)
in(1)
it(1)
mankind(1)
necessary(1)
one(1)
opinions(1)
people(1)
political(1)
powers(1)
requires(1)
respect(1)
separate(1)
separation.(1)
should(1)
station(1)
that(1)
them,(1)
they(1)
thirteen(1)
unanimous(1)
united(1)
with(1)
=======================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.