question in c++ data structure

Discussion in 'Programmer's Corner' started by moslem, Apr 30, 2010.

  1. moslem

    Thread Starter New Member

    Dec 16, 2009
    20
    0
    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.
     
  2. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    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
     
  3. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    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... :)

    Code ( (Unknown Language)):
    1.  
    2. import sys
    3. text, word_counts = sys.stdin.read(), {}
    4. for word in text.split():
    5.     if word in word_counts:
    6.         word_counts[word] += 1
    7.     else:
    8.         word_counts[word] = 1
    9. keys = word_counts.keys()
    10. keys.sort()
    11. fmt = "%%-%ds %%s" % max([len(i) for i in keys])
    12. for word in keys:
    13.     print fmt % (word, "*"*word_counts[word])
    14.  
     
  4. jamjes

    Member

    May 10, 2010
    35
    0
Loading...