-
Notifications
You must be signed in to change notification settings - Fork 0
/
yetanotherht.c
72 lines (63 loc) · 1.42 KB
/
yetanotherht.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
#include <stdio.h>
#include <string.h>
#include "yaht.h"
#include "yaht.inl.h"
int main(int argc, char* argv[]);
//Test function
char * strrev(char *str)
{
int i = strlen(str) - 1, j = 0;
char ch;
while (i > j)
{
ch = str[i];
str[i] = str[j];
str[j] = ch;
i--;
j++;
}
return str;
}
static char * mystrdup(const char * str)
{
char * tmp;
tmp = calloc(strlen(str) + 1, 1); //calloc/debug
strcpy(tmp, str);
return tmp;
}
int main(int argc, char* argv[])
{
FILE * pfWords = stdin; //fopen("words.txt", "rb");
PHashTable pHT = Hash_Create();
char line[2048];
while (fgets(line, sizeof(line), pfWords))
{
if (strlen(line))
line[strlen(line) - 1] = '\0';
{
char * pTmp;
//Collision check
if ( (pTmp = Hash_LookupStr(pHT, line)) )
{
printf("Collision found!\r\n->\t\"%s\"\r\n->\t\"%s\"\r\n", pTmp, line);
}
else
{
char * dup = strrev(mystrdup(line));
Hash_AddStrKey(pHT, line, dup);
}
}
}
fprintf(stderr, "Item count: %d\r\n", pHT->count);
if (argc > 1)
fprintf(stderr, "Lookup item: %s -> %s\r\n", argv[1], Hash_LookupStr(pHT, argv[1]));
if (argc > 2)
{
PHashItem pItem;
for (pItem = pHT->pListHead; pItem; pItem = pItem->pNext)
puts(pItem->pItem);
}
Hash_Destroy(pHT);
fclose(pfWords);
return 0;
}