0% found this document useful (0 votes)
11 views2 pages

Untitled

This document implements the functions for a dictionary data structure using a hash table with chaining. It includes functions to check if a word is in the dictionary, hash words to map to table indices, load the dictionary from a file into the hash table, return the number of words, and unload the dictionary (not implemented). Key aspects are using a node struct to represent entries in each chain, a hash table of these nodes, and functions for checking words, hashing keys, and loading/accessing the dictionary contents.

Uploaded by

Andrew Olsen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Untitled

This document implements the functions for a dictionary data structure using a hash table with chaining. It includes functions to check if a word is in the dictionary, hash words to map to table indices, load the dictionary from a file into the hash table, return the number of words, and unload the dictionary (not implemented). Key aspects are using a node struct to represent entries in each chain, a hash table of these nodes, and functions for checking words, hashing keys, and loading/accessing the dictionary contents.

Uploaded by

Andrew Olsen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "dictionary.h"
int sizes = 0;

// Represents a node in a hash table


typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;

// TODO: Choose number of buckets in hash table


const unsigned int N = 26*26;

// Hash table
node *table[N];
node *checkp;
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
checkp = table[hash(word)];
while(checkp != NULL)
{
if(!strcasecmp(checkp->word, word))
{
return true;
}
checkp = checkp->next;
}
return false;
}

// Hashes word to a number


unsigned int hash(const char *word)
{
// TODO: Improve this hash function
return ((26*(toupper(word[0]) - 'A')) + (toupper(word[1]) - 'A'));
}
FILE *fptr;
char word[50];
int check_end;
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
fptr = fopen(dictionary, "r+");
if (fptr == NULL)
{
return 0;
}
else
{
check_end = hash(word);
while(fscanf(fptr, "%s", word) != EOF)
{
if (fptr == NULL)
{
return 0;
}
else
{
node *new = malloc(sizeof(node));
if (new == NULL)
{
return 0;
}
strcpy(new->word, word);
new->next = NULL;
new->next = table[check_end]->next;
table[check_end] = new;
sizes++;
}
}
}
return true;
}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded


unsigned int size(void)
{
return sizes;
}

// Unloads dictionary from memory, returning true if successful, else false


bool unload(void)
{
// TODO
return false;
}

You might also like