Skip to content

Instantly share code, notes, and snippets.

/dictionary.h Secret

Created September 12, 2016 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6c71126b2fcea6d81871683e6f8f3e48 to your computer and use it in GitHub Desktop.
Save anonymous/6c71126b2fcea6d81871683e6f8f3e48 to your computer and use it in GitHub Desktop.
/**
* dictionary.h
*
* Computer Science 50
* Problem Set 5
*
* Declares a dictionary's functionality.
*/
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <stdbool.h>
// maximum length for a word
// (e.g., pneumonoultramicroscopicsilicovolcanoconiosis)
#define LENGTH 45
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word);
// count the number of words in dictionary
unsigned int count = 0;
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary);
// define hash function
int hash_it(char* needs_hashing)
{
unsigned int hash = 0;
for (int i = 0, n = strlen(needs_hashing); i<n; i++) hash = (hash << 2) ^ needs_hashing[i];
return hash % HASHTABLE_SIZE;
}
// define node in hashtable
typedef struct node
{
char word[LENGTH + 1];
struct node* next = NULL;
}
node;
// define hash table
node* hashtable[65536];
for (int i = 0; i < 65536; i++) hashtable[i] = NULL;
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void);
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void);
#endif // DICTIONARY_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment