1

I have hit the wall with the LOAD FUNCTION of PSet6. Does the following pseudocode look correct? Thanks for guidance.

  1. open dictionary file.
  2. create a node struct
  3. create an array of list_pointers (our hash table)
  4. create a hash function
  5. read dictionary file one character at a time putting each character into a buffer until a \0.
  6. put buffer in as argument to the hash function.
  7. take integer value recevied from hash function and go to list_pointer[integer].
  8. if there is no word yet add to end of list pointer.
  9. if there is a word, then there is a linked list of one or more words. then traverse the list comparing each character of buffer to word in struct of linked list until you find the right place to insert. compare buffer[i] to node.word[i] if buffer[i] < node.word[i] go to buffer[i +1] and compare again if buffer[i] > node.word[i] insert word if buffer[i] == buffer[\0] then end of word is reached and word is already in dictionary return false
  10. when EOF is reached for dictionary, free memory for creating a node. end function.
2

For steps 2-3, the data structure (e.g., the hash table in your case) that's gonna be used to store the words in the dictionary and the array of pointers to variables of that type both should be created/declared as global not local to load().

For step 5, as since you're reading char-by-char, you should stop as you hit a newline character as each word in the dictionary ends with a newline character (not with a null terminator). After you hit a newline character, you should stop reading and add a null byte (aka the null terminator) at the end of the word, hash it and insert it.

For step 9, probably a better way to compare words is to use strcmp() (declared in string.h) instead of individually comparing the chars of each word.

For step 10, I don't know exactly which node you're trying to free, but if you reach the EOF, you probably shouldn't be freeing anything. The only case in which you're supposed to free something while loading is probably when the word is rejected.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .