-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc_dict.c
72 lines (63 loc) · 1.58 KB
/
wc_dict.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
// Thomas Stitt - CMPSC 473 - Project 3 - 4/5/15
#include "wc_dict.h"
unsigned hash(char *s) {
unsigned hashval;
for (hashval = 0; *s != '\0'; s++) {
hashval = *s + 31 * hashval;
}
return hashval % DICTSIZE;
}
// iterate over the list at the hash of s and then strcmp
// and attempt to find s
entry* lookup(entry** dict, char* s)
{
entry* e;
for (e = dict[hash(s)]; e != NULL; e = e->next) {
if (strcmp(s, e->word) == 0) {
return e;
}
}
return NULL;
}
int insert(entry** dict, char* word, int count) {
entry* e;
unsigned hashval;
// not found, create entry and set to count
if ((e = lookup(dict, word)) == NULL) {
e = (entry*)malloc(sizeof(entry));
// dictionary error
if (e == NULL || (e->word = strdup(word)) == NULL) {
fprintf(stderr, "Problem adding to dictionary\n");
return 1;
}
e->wc = count;
hashval = hash(word);
e->next = dict[hashval];
dict[hashval] = e;
}
// found, increment
else {
e->wc += count;
}
return 0;
}
// Combine dict2 into dict1
int combine(entry** dict1, entry** dict2) {
int e;
entry* p;
for (e = 0; e < DICTSIZE; e++) {
for (p = dict2[e]; p != NULL; p = p->next) {
insert(dict1, p->word, p->wc);
}
}
return 0;
}
void dictionary_print(entry** dict) {
int e;
entry* p;
for (e = 0; e < DICTSIZE; e++) {
for (p = dict[e]; p != NULL; p = p->next) {
printf("%s - %d\n", p->word, p->wc);
}
}
}