-
Notifications
You must be signed in to change notification settings - Fork 2
/
problem_set5.c
227 lines (177 loc) · 4.44 KB
/
problem_set5.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Implements a dictionary's functionality
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.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 = 100000;
// Hash table.
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
//create lowercase copy of word
char word_lower[LENGTH + 1];
for (int i = 0, l = strlen(word); i <= l; i++)
{
word_lower[i] = tolower(word[i]);
}
//initialise temporary pointer.
node *temp = table[hash(word_lower)];
//go through linked list and search for word
while (temp != NULL)
{
if (strcmp(temp -> word, word_lower) == 0)
{
return true;
}
else
{
temp = temp -> next;
}
}
return false;
}
// Hashes word to a number
//https://stackoverflow.com/questions/2624192/good-hash-function-for-strings
unsigned int hash(const char *word)
{
int hash = 7;
for (int i = 0, l = strlen(word); i < l; i++)
{
hash = (hash * 31 + word[i]);
}
return hash % N;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
//open dictionary file
FILE *fp_dictionary = fopen(dictionary, "r");
if (fp_dictionary == NULL)
{
printf("Failed to fopen %s\n", dictionary);
return false;
}
//intialise variables
char word[LENGTH + 1];
int index = 0;
//ensure hash table starts with null pointers
for (int i = 0; i < N; i++)
{
table[i] = NULL;
}
//go through dictionary file adding words to hashtable
for (int c = fgetc(fp_dictionary); c != EOF; c = fgetc(fp_dictionary))
{
//if we have newline character then we are at end of word. add word to hashtable
if (c == 10)
{
word[index] = '\0';
index = 0;
bool loaded = load_word(word);
if (!loaded)
{
printf("Could not load %s.\n", word);
return false;
}
}
//if no newline character, add character to end of word
else
{
word[index] = c;
index++;
}
}
//close file
fclose(fp_dictionary);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
//initialise variables
int counter = 0;
node *temp = NULL;
//loop through hash table, incrementing counter by 1 for each non-null pointer
for (int i = 0; i < N; i++)
{
temp = table[i];
while (temp != NULL)
{
counter++;
temp = temp -> next;
}
}
return counter;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
//initialise variables. by construction, temp_prev -> next = temp
node *temp = NULL;
node *temp_prev = NULL;
//loop through table, freeing all the mallocs
for (int i = 0; i < N; i++)
{
temp = table[i];
while (temp != NULL)
{
temp_prev = temp;
temp = temp -> next;
free(temp_prev);
}
}
return true;
}
//adds word to hash table
bool load_word(const char *word)
{
//initalise temporary pointer
node *temp = table[hash(word)];
//need sepearate case for creating first item in the list
if (temp == NULL)
{
//allocate new block of memory
temp = malloc(sizeof(node));
//check for null
if (temp == NULL)
{
printf("Malloc for new node failed\n");
return false;
}
//fill node with data
temp -> next = NULL;
strcpy(temp -> word, word);
//update pointer in table
table[hash(word)] = temp;
return true;
}
//get to end of linked list
while (temp -> next != NULL)
{
temp = temp -> next;
}
//allocate new block of memory.
temp -> next = malloc(sizeof(node));
temp = temp -> next;
//check for null
if (temp == NULL)
{
printf("Malloc for new node failed\n");
return false;
}
//fill node with data
temp -> next = NULL;
strcpy(temp -> word, word);
return true;
}