-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.c
53 lines (41 loc) · 1.2 KB
/
hashtable.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
#include "hashtable.h"
int hash(HashTable hTable, unsigned long hashValue)
{
return (hashValue%hTable->M);
}
HashTable initHashTable(int M)
{
int i;
HashTable newHTable = (HashTable) malloc(sizeof(struct hashtable));
newHTable->M = M;
newHTable->pointerLists = (PointerList*) malloc(sizeof(PointerList)*M);
for (i = 0; i < M; i++) {
newHTable->pointerLists[i] = initPointerList();
}
return newHTable;
}
void freeLinkHashTable(HashTable hTable, PointerLink pointerLink)
{
int i = hash(hTable, getId(getItem(getItemLink(pointerLink))));
freePointerLink(hTable->pointerLists[i], pointerLink);
}
void freeHashTable(HashTable hTable)
{
int i;
for (i = 0; i < hTable->M; i++) {
freePointerLinks(hTable->pointerLists[i]);
}
free(hTable->pointerLists);
free(hTable);
}
void addLinkToHash(HashTable hTable, ItemLink itemLink)
{
Item item = getItem(itemLink);
int i = hash(hTable, hashKey(item));
addPointerToList(hTable->pointerLists[i], itemLink);
}
PointerLink findLinkInHash(HashTable hTable, unsigned long searchValue)
{
int i = hash(hTable, searchValue);
return findPointerLink(hTable->pointerLists[i], searchValue);
}