-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_chain.h
More file actions
179 lines (144 loc) · 3.68 KB
/
hash_chain.h
File metadata and controls
179 lines (144 loc) · 3.68 KB
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
#ifndef HASH_CHAIN_H_INCLUDED
#define HASH_CHAIN_H_INCLUDED
#define MAX 25
#define MAX_DISP 10
#define TRUE 1
#define FALSE 0
typedef int bool;
typedef struct {
int *table;
int maxLen;
} HashTable;
typedef struct {
int value;
struct HNode *next;
} HNode;
void createHashTable_Chain(HashTable **table, int max) {
if (max == NULL) {
max = MAX;
}
(*table) = malloc(sizeof(HashTable));
(*table)->maxLen = max;
(*table)->table = malloc(sizeof(HNode*) * max);
for (int i=0; i<max; i++) {
((*table)->table)[i] = NULL;
}
}
void insertToTableToLinkedList(HNode **head, int value) {
HNode *newHNode = malloc(sizeof(HNode));
newHNode->next = NULL;
newHNode->value = value;
if ((*head) == NULL) {
(*head) = newHNode;
return;
}
HNode *t = (*head);
while (t->next != NULL)
t = t->next;
t->next = newHNode;
}
int hash(int max, int key) {
return key % max;
}
void insertToTable(HashTable **table, int key, int value) {
if ((*table) == NULL)
return;
HNode *head = NULL;
int idx = hash((*table)->maxLen, key);
if ((*table)->table[idx] == NULL) {
insertToTableToLinkedList(&head, value);
(*table)->table[idx] = head;
} else {
insertToTableToLinkedList(&((*table)->table[idx]), value);
}
}
void printKey(HashTable *table, int key, int maxDisp) {
if (maxDisp == NULL)
maxDisp = MAX_DISP;
int idx = hash(table->maxLen, key);
HNode *head = table->table[idx];
if (head == NULL)
return;
displayList(head, maxDisp);
}
bool searchList(HNode *head, int value) {
HNode *t = head;
while (t != NULL) {
if (t->value == value)
return TRUE;
t = t->next;
}
return FALSE;
}
int searchListIdx(HNode *head, int value) {
int count = 0;
HNode *t = head;
while (t != NULL) {
if (t->value == value)
return count;
t = t->next;
count++;
}
return -1;
}
void displayList(HNode *head, int maxDisp) {
int i = 0;
HNode *t = head;
while (t != NULL && i < maxDisp) {
printf("%d -> ", t->value);
t = t->next;
i++;
}
printf("\n");
}
void printTable(HashTable *table, int maxDisp) {
if (maxDisp == NULL)
maxDisp = MAX_DISP;
for (int i=0; i<table->maxLen; i++) {
HNode *head = table->table[i];
printf("[%d] --- ", i);
if (head == NULL)
printf("~~\n");
else
displayList(head, maxDisp);
}
}
bool searchTable(HashTable *table, int key, int value) {
int idx = hash(table->maxLen, key);
if (table->table[idx] == NULL)
return FALSE;
return searchList(table->table[idx], value);
}
void removeKey(HashTable **table, int key) {
int idx = hash((*table)->maxLen, key);
(*table)->table[idx] = NULL;
}
HNode *removeHNode(HNode *head, int value) {
HNode *t = head, *prev = head;
// If list is empty
if(t == NULL)
return NULL;
// If head will be removed
if(t != NULL && t->value == value) {
head = head->next;
free(t);
return head;
}
// Iterate t to the HNode that will be removed
while(t->next != NULL && t->value != value) {
prev = t;
t = t->next;
}
// If HNode was found, remove
if(t->value == value) {
prev->next = t->next;
free(t);
}
return head;
}
void removeValue(HashTable **table, int key, int value) {
int idx = hash((*table)->maxLen, key);
HNode *head = (*table)->table[idx];
(*table)->table[idx] = removeHNode(head, value);
}
#endif // CHAIN_HASHING_H_INCLUDED