-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.c
69 lines (60 loc) · 1.58 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include "HashTable.h"
// Create a hash array of n tables
// as well as allocate the required memory
Hashtable *hash_create(int n) {
int i;
Hashtable *hash = malloc(sizeof(Hashtable));
hash->tables = n;
hash->hashtable = malloc(n*sizeof(node));
for (i = 0; i < n; i++) {
hash->hashtable[i] = NULL;
}
return hash;
}
// Check if the id has already been inserted
// Then insert the new id into the hashtable
void hash_insert(Hashtable *ht, Graph *graph, int id) {
int n = ht->tables;
add_node(&(ht->hashtable[id%n]), graph, id);
}
// Clear a specific account from the hashtable
void hash_delete(Hashtable *ht, int id) {
int n = ht->tables;
delete_node(&(ht->hashtable[id%n]), id);
}
// Clear all the hashtable nodes
void hash_destroy(Hashtable *ht) {
int i;
int n = ht->tables;
for (i = 0; i < n; i++) {
destroy_list(&ht->hashtable[i]);
}
for (i = 0; i < n; i++) {
ht->hashtable[i] = NULL;
}
// Use here only if user input gets removed
// free(ht->hashtable);
// free(ht);
}
// Get a graph with the specified id
Graph *hash_getBucket(Hashtable *ht, int id) {
int n = ht->tables;
return get_node(ht->hashtable[id%n], id);
}
// Print the whole hashtable
void hash_printall(Hashtable *ht) {
int i;
int size = ht->tables;
for (i = 0; i < size; i++) {
if (ht->hashtable[i] != NULL) {
print_list(ht->hashtable[i]);
}
}
}
// Search if if an account with id is inside the table
int hash_search(Hashtable *ht, int id) {
int n = ht->tables;
return find_node(ht->hashtable[id%n], id);
}