Homework Assignment 8

Assigned: Monday, April 7, 1997.
New Due Date: Wednesday, Apr 23, 1997.

Read: Read Chapters in the class text, 9, 11, 12 new edition (or Chapters 10, 11, 12 and 14 old edition) and study carefully the example programs:

Program: You are to write the UniqueWords.C program. Invoked with a file name on the command line, the program opens the file, reads the contents, parses the individual words in the file, and keeps a list of all the words in the file, each word in the list once, with a count of how many times the word appears in the file.

For instance, the program UniqueWords on the file:

  one fish
  two fish
  red fish
  blue fish
should give the output,
  count word
  -----+-----------------
     1   blue
     1   red
     1   two
     4   fish
     1   one
Actually, the order of the words doesn't matter, just that the count is correct.

This program is very much like the program SomeUniqueWords.C except that you are to use a linked list, rather than an array, and to keep a count of the number of times each word appears in the file.

The list structure is detailed elsewhere.

First accomplish this program adding new words to the head of the list. The problem with this method is that frequent words end up at the back of the list, making searching slow. You are next ot write MoveToFront.C which implements the Move To Front algorithm to speed up the program.

The Move To Front algorithm says that each time that you look up a word, if the word is found, increment its count and remove the word from its present location in the list and reinsert it at the head of the list. New words are inserted at the head of the list, as always. The point is that words used frequently are frequently brought to the front, and are therefore never found very far from the front. Searches on the most popular words never go very deep into the list. This speeds up the program immensely.

Modify UniqueWords.C to MoveToFront.C and compare their speeds on the sample text Constitution.txt and on Declaration.txt.