How to count word frequency in linked list from standard input C++.?
-
I have a program that currently I input words into and it sorts them as I add them. I also need to create a function that counts the frequency of each word and then displays the each word once with it's frequency next to it. I'm close except it not quite right and I can't figured out how to fix it. Here's my code so far: (the display function is where I'm having the trouble counting and displaying correctly) #include <cstddef> #include <iostream> #include <string> using namespace std; template <class T> class LinkedList { private: struct ListNode{ T value; ListNode *next; }; ListNode *head_; public: LinkedList(){ head_ = NULL; } void insertToHead(T value); void bubbleSort(); void display() const; void count(); ~LinkedList(); }; //Inserts the the word to the head of the list. template <class T> void LinkedList<T>::insertToHead(T value){ ListNode *newNode = new ListNode; newNode->value = value; newNode->next = head_; head_ = newNode; } //Displays the word once and the number of times it occurs in the list. void LinkedList<string>::display() const{ int count=1; ListNode *temp = head_; cout << "List: " << "Frequency: " << endl; while (temp != NULL){ if (temp->value != (temp->next)->value){ cout << temp->value << " " << count << endl; temp = temp->next; } while (temp->next != NULL && temp->value == (temp->next)->value){ count++; temp = temp->next; } cout << temp->value << " " << count << endl; count = 1; } cout << endl; } //After finished using the List it deconstructs the list and reallocates the memory. template <class T> LinkedList<T>::~LinkedList(){ ListNode *temp; while (head_ != NULL) { temp = head_; head_ = head_->next; delete temp; } } // Sorts the list into alphabetical order. void LinkedList<string>::bubbleSort(){ int i, j, n=0; string hold; ListNode *q, *p, *t; for (ListNode *q = head_; q; q=q->next) ++n; for (i=1, t=head_; i<=n-1; t=t->next, ++i) for(j=0, p=head_; j<n-i; p=p->next, ++j) if (p->value > (p->next)->value){ hold = p->value; p->value = (p->next)->value; (p->next)->value = hold; } } // takes in a string and inserts then sorts it in a list until // the string is a "#" and then displays the List with the // number of frequency of each word. int main(){ LinkedList<string> sList; LinkedList<int> countList; LinkedList<string> finalList; string word; cin >> word; while (word != "#"){ sList.insertToHead(word); sList.bubbleSort(); cin >> word; } sList.display(); } Sample Input: one two two three Output: one 1 three 1 three 1 two 2 Then it has an error and stops! help please!
-
Answer:
Type Wordfreq... Good luck!
cma5357 at Yahoo! Answers Visit the source
Related Q & A:
- How To Count Sleeps Till Christmas?Best solution by terrycassell.com
- How to set CPU frequency to highest?Best solution by Ask Ubuntu
- how to draw circular doubly linked list?Best solution by TeX - LaTeX
- How to programatically select a item in list using c#?Best solution by Software Quality Assurance & Testing
- How to traverse linked list in c++?Best solution by algolist.net
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.