-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_word.c
148 lines (130 loc) · 2.97 KB
/
hash_word.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_TABLE_SIZE 100000
typedef struct WordNode {
char* word;
int count;
struct WordNode* next;
} WordNode;
WordNode* create_word_node(char* word) {
WordNode* node = (WordNode*) malloc(sizeof(WordNode));
node->word = (char*) malloc(strlen(word) + 1);
strcpy(node->word, word);
node->count = 1;
node->next = NULL;
return node;
}
void insert_word(WordNode** hash_table, char* word) {
unsigned long hash_value = 5381;
int c;
char* temp = word;
while ((c = *temp++)) {
hash_value = (hash_value << 5) + c;
}
int index = hash_value % HASH_TABLE_SIZE;
printf("The word [%s] hash value is %ld, %d\n", word, hash_value, index);
WordNode* node = hash_table[index];
if (node == NULL) {
hash_table[index] = create_word_node(word);
} else {
while (node != NULL) {
if(strcmp(node->word, word) == 0) {
node->count++;
return;
}
if(node->next == NULL) {
break;
}
node = node->next;
}
node->next = create_word_node(word);
//node->count++;
}
}
void print_word_list(WordNode** hash_table) {
WordNode* node;
int i, j;
WordNode* sorted_list[HASH_TABLE_SIZE] = {NULL};
if (hash_table == NULL) {
printf("Word list is empty.\n");
return;
}
printf("Not empty.\n");
/*
for (i = 0; i < HASH_TABLE_SIZE; i++) {
node = hash_table[i];
while (node != NULL) {
WordNode* temp = sorted_list[node->count];
if(temp == NULL) {
sorted_list[node->count] = node;
} else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = node;
}
node = node->next;
}
}
for (i = HASH_TABLE_SIZE - 1; i >= 0; i--) {
node = sorted_list[i];
while (node != NULL) {
printf("%s, %d\n", node->word, node->count);
node = node->next;
}
}
*/
for(i = 0; i < HASH_TABLE_SIZE; i++) {
//printf("#####%d, Not empty.\n", i);
node = hash_table[i];
while(node != NULL) {
printf("%s, %d\n", node->word, node->count);
node = node->next;
}
}
}
/*
int isNull(char c) {
return (c >=65 && c <=122) ? 1 : 0;
}
int getWord(FILE* fp, char* str) {
char c;
int counter = 0;
while((c = fgetc(fp)) != EOF){
if( isNull(c) && (counter <= 0)){
continue;
}
else if(isNull(c) && (counter > 0)){
break;
}
str[counter++] = c;
}
str[counter] = '\0';
if(counter > 0)
return 1;
else
return 0;
}
*/
int main() {
FILE* fp;
char word[1024];
WordNode* hash_table[HASH_TABLE_SIZE] = { NULL };
int word_count = 0;
fp = fopen("./input.txt", "r");
if (fp == NULL) {
printf("Failed to open input file.\n");
return 1;
}
while (fscanf(fp, "%s", word) != EOF) {
insert_word(hash_table, word);
//printf("Have a look: %s\n", word);
word_count++;
}
printf("Total words: %d\n", word_count);
printf("Word list:\n");
print_word_list(hash_table);
fclose(fp);
return 0;
}