I have tried to implement a code that does the following:
- store words in an array (unordered)
- use function
order_array()to place repetitions for the same word near (so the result, given a sample text file, could be:mom mom mom mom lamb lamb eye eye pain tears) - use function
count_frequencyto store every word one single time + its frequency (I store them in a linked list) - print the linked list to obtain given result
The code works, but it appears (to my eye) cumbersone and unefficient. How can I improve it?
Note that I am not familiar with concepts like vectors, hash maps or dictionaries yet, but I have a good grasp of basic data structures (eg linked lists, stacks, queues, binary search trees). Still, any hint, help or advice would be greatly appreciated
using namespace std;
#include<iostream>
#include<fstream>
#include<string>
const int DIM = 50;
int count = 0;
string list[DIM];
string ordered_list[DIM];
struct node
{
string word;
int frequency;
node* next;
};
node* head = NULL;
node* remove_node(node* &h)
{
node* s = head;
head = s->next;
return s; // and then remove everything
}
void add_node(string &w, int &f, node* &h)
{
node* t = new node;
t->word = w;
t-> frequency = f;
t->next = h;
h = t;
}
void print_highestf(node* &h)
{
node* s = h;
int higherf = 0;
while(s!=NULL)
{
if((s->frequency) >= higherf)
{
higherf = s->frequency;
}
s = s->next;
}
cout << "The highest frequency for a word is: " << higherf << endl;
}
void print_res(node* &h)
{
for(node* s = head; s != NULL; s = s->next)
{
cout << "Frequency for word " << s->word << ": " << s->frequency << endl;
cout << "\n";
}
}
void upload_array(string &w, string a[])
{
a[count] = w;
count ++;
}
bool is_new_string(string &word, int position) //check better
{
bool repeated = false;
for(int i = 0; i < position; i++)
{
if(word == list[i] && repeated == true)
{
return false;
}
else if(word == list[i] && repeated == false)
{
repeated = true;
}
}
return true;
}
void order_array(string a[], string oa[])
{
int i = 0;
int times = 0;
while(i!= count)
{
int j = i;
while(j != count)
{
if((a[i] == a[j]) && is_new_string((a[i]), i+1))
{
//new node
oa[times]=a[i];
times++;
}
j++;
}
i++;
}
}
void count_frequency(string oa[])
{
int frequency = 1;
for(int i = 0; i<count-1; i++)
{
if(oa[i] != oa[i+1])
{
add_node(oa[i], frequency, head);
frequency = 1;
}
else if(oa[i] == oa[i+1])
{
frequency++;
}
}
}
int main()
{
fstream mystream;
mystream.open("words.txt", ios::in);
string word;
//upload array of strings
while(!mystream.eof())
{
mystream >> word;
upload_array(word, list);
}
order_array(list, ordered_list);
count_frequency(ordered_list);
print_res(head);
print_highestf(head);
for(node* s= head; s!=NULL; s = s->next)
{
node* r = remove_node(head);
delete r;
}
cout << "\nDeinitialization executed. " << endl;
mystream.close();
return 0;
}