question in c++ data structure

Thread Starter

moslem

Joined Dec 16, 2009
20
hi every one
i want to write aprogram that read afile and stores the words in the list(linked list) and returns the number of occurrences of each word in the file,
reading file is easy to me but i want to help me howa to print out number of occurrences of each word.
thanks.
 

BMorse

Joined Sep 26, 2009
2,675
Depending on the file structure, each "word" should be separated by a space, so when reading the file, search for the space before and after to get the whole word, then compare this word to the words in the list and increment a count for each word if another occurrence is detected.

My .02
 

someonesdad

Joined Jul 7, 2009
1,583
Using a linked list for this task doesn't make any sense. Can you explain your logic or (if this is a homework assignment) give the exact wording of the problem?

This type of task is very common in computer tasks and other data structures are more appropriate, such as trees, hash tables, and maps/dictionaries.

If I had to do this in C++ and I had the STL available, I'd use the STL map. The coding would be trivial -- just a few lines of code. You'll find the accurate breaking up of English into words a much more difficult problem, at least if you want to handle the general case and write a useful, robust tool.

If you're in a data structures class, then your teacher might want you to implement these data structures. That's a harder task, yet everyone should learn how to program lists, maps, hash tables, trees, etc. at the bare metal level once, since it's a good learning experience and gives you insight on applying these structures later.

Here's a naive implementation in python; it makes a histogram and prints the words in alphabetical order. It's easy to change to other output forms. Run it on some English text and you'll see why the problem of splitting things into words isn't as easy as you might think it should be... :)

Rich (BB code):
import sys
text, word_counts = sys.stdin.read(), {}
for word in text.split():
    if word in word_counts:
        word_counts[word] += 1
    else:
        word_counts[word] = 1
keys = word_counts.keys()
keys.sort()
fmt = "%%-%ds %%s" % max([len(i) for i in keys])
for word in keys:
    print fmt % (word, "*"*word_counts[word])
 
Top