-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDictionary-DataStructures.c
111 lines (100 loc) · 2.18 KB
/
Dictionary-DataStructures.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Implements a dictionary's functionality
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 26;
// Hash table
node *table[N];
// Size of the dictionary
int dict_size = 0;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int h = hash(word);// Hashes given word
node *temp = table[h];
while (temp != NULL)
{
if (strcasecmp(word, temp->word) == 0)// Compares words case-insensitively
{
return true;
}
temp = temp->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
unsigned int h;
h = toupper(word[0]) - 65;
return h;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary, "r"); // Opens file in read mode
if (dict == NULL)
{
return false;
}
char dword[LENGTH + 1];
while (fscanf(dict, "%s", dword) != EOF) // Reads through the file
{
int h = hash(dword);
node *in = malloc(sizeof(node));
if (in == NULL)
{
return false;
}
strcpy(in->word, dword);
if (table[h] == NULL)
{
table[h] = in;
table[h]->next = NULL;
}
else
{
in->next = table[h];
table[h] = in;
}
dict_size++;
}
fclose(dict);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
if (dict_size > 0)
{
return dict_size;
}
return 0;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *t = table[i];
while (t != NULL)
{
node *d = t;
t = t->next;
free(d); // Frees memory
}
}
return true;
}