Translation Suggestions

CSC 7000-002 Algorithms & Programming
Fall 1999, Mendel 115
Dr. David Matuszek

A few people have expressed confusion about just how to do this assignment. Here are some more specific guidelines.

First, create a struct named cell with three fields, as follows:

struct cell{char *english; char *german; struct cell *next;};
typedef struct cell cell;

Next, write the following routines:

cell *create_list ();
cell *add (char *english_word, char *german_word, cell *list);
char *find (char *english_word, cell *list);

Here you write the necessary code to define a linked list. The struct cell defines a block of storage with room for two pointers to strings and a pointer to another struct cell. The function create_list() "creates" an empty list (which is very little work!), and the function add() will

  1. malloc() space for the cell,
  2. malloc() some more space and make copies of the two strings (why?),
  3. fill the new cell with pointers to the copied strings and to the (first cell of the) old list, and
  4. return a pointer to the new cell, which is now the first element of the linked list.

The function find() just searches a list for the English word and returns the corresponding German word.

Define a type dictionary as follows:

typedef cell *dictionary[26];

Write the following routines:

void add_to_dictionary (char *english_word, char *german_word, dictionary dict);
char *look_up (char *english_word, dictionary dict);

A dictionary is an array of 26 locations, each of which is a pointer to a linked list. To add an entry to the dictionary, add it to the correct linked list. To find the correct linked list, do some arithmetic: chars are a kind of integer, so (letter - 'a') is all you need. Once you've determined the correct array location, adding the entry is no work at all, just use add(). Similarly, looking up an entry should be no work at all.

Your main program:

  1. Reads in pairs of words from an input file and adds them to the dictionary.
  2. Reads in a second input file (probably line-by-line), translates each word, and writes the translation to a second file.

Note that the main program talks to the dictionary routines, and the dictionary routines talk to the linked-list routines, but the main program doesn't talk to the linked list routines.

Reasonable variation: you might decide that it makes more sense to have add_to_dictionary() make copies of the strings, rather than add().