Ok so I need to find one word for each letter [a..z] which is the most commonly used for that letter. I thought of creating an array of linked lists, where words for each letter are placed in list, if the word is already in the list,word counter add 1 for count. But after that, when is have all of them in seperate lists, how do I print one words for each letter? Maybe there is easier way how to do this?
Sorry I don't know how to use std::map and it will be too difficult for me. So here is what i have now. It compiles, bet when I try to cout something it says "program.exe has stopped working"
For example:
struct word
{ string vaards;
int skaits;
word *next;
};
When you create word* p, you don't allocate any memory for an actual struct word, so when you do p->vaards you are writing to random memory; wherever p happens to be pointing.
Ok I got this, thanks. Could you please take a look at my code and tell me what is wrong?
It seems to compile, but when i try to cout something, it shows nothing, for example at the end:
for (p[0]=first[0]; p[0]!=NULL; p[0]=p[0]->next)
cout << p[0]->vaards; // -> nothing, but it should be
#include <iostream>
#include <fstream>
using namespace std;
struct word
{ string vaards;
int skaits;
word *next;
};
int main()
{
string s;
word *first[26];
word *last[26];
word *p[26];
int x;
for (int i=0; i<26; i++)
{
first[i]=last[i]=NULL;
}
fstream fin ("D:\\pilseta.txt", ios::in);
fin >> s;
bool original=true;
while (fin)
{
if (s[0]>'A' && s[0]<'Z') s[0]=s[0]+'a'-'A';
x=s[0]-'a';
for (p[x]=first[x]; p[x]!=NULL; p[x]=p[x]->next)
if (s==p[x]->vaards)
{
original=false;
p[x]->skaits++;
}
if (original==true)
{
p[x] = new word;
p[x]->vaards=s;
p[x]->skaits=1;
p[x]->next=NULL;
if (first[x]==NULL) first[x]=last[x]=NULL;
else
{
last[x]->next=p[x];
last[x]=last[x]->next;
}
};
fin >> s;
}
fin.close();
for (p[0]=first[0]; p[0]!=NULL; p[0]=p[0]->next)
cout << p[0]->vaards;