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
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:
// // linked List Main Program include#include #include #include #include using namespace std; class wordNode { public: wordNode(string word, count, wordNode* nextNodePtr); void InsertAfter(wordNode* nodePtr); wordNode* GetNextPtr(); int GetCount(); string GetWord(); private: string word; int count; wordNode* nextNodePtr; }; wordNode(string inWord, int inCount, wordNode* inNextNodePtr = nullptr) { word = inWord; count = inCount; nextNodePtr = inNextNodePtr; }; // // swap the contents of two wordNode objects // void swapWordNodes(wordNode* wordNode1, wordNode* wordNode2){ } // // Bubble Sort by wordNode count // void orderByCount(wordNode* firstNode){ wordNode* lastNode = getLastNodeAddr(); wordNode* currentNode = nullptr; wordNode* nextNode = nullptr; wordNode* newLastNode = nullptr; while (firstNode != lastNode){ currentNode = firstNode; while (currentNode != lastNode){ nextNode = current-Node>GetNext(); if (currentNode->GetCount() < nextNode->GetCount()) swapWordNodes(currentNode, nextNode); newLastNode = currentNode; currentNode = current->GetNext(); }; lastNode = newLastNode; }; } // // Print out words in the linked list in alphabetical orde along with the word count. // void printAlphaOrder(wordNode* list){ } // // Print out words in the linked-list in count order from highest to lowest count // void printCountOrder(wordNode* list){ } // // 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; // // 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 // // // 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 // } The 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.
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
