-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashMap.c
158 lines (139 loc) · 3.45 KB
/
HashMap.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
148
149
150
151
152
153
154
155
156
157
158
// Harris Ransom
// C Hashmap
// Based on video by Jacob Sorber
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_NAME 256
#define TABLE_SIZE 10
#define DELETED_NODE (person*) (0xFFFFFFFFFFFFFFFFUL)
typedef struct {
char name[MAX_NAME];
int age;
} person;
person* hashTable[TABLE_SIZE] = { NULL };
// Initializes hash table
void initHashTable() {
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable[i] = NULL;
}
return;
}
// Simple hash function (add, multiply, mod)
unsigned int hash(char *name) {
int length = strnlen(name, MAX_NAME);
unsigned int hashVal = 0;
for (int i = 0; i < length; i++) {
hashVal += name[i];
hashVal *= name[i];
hashVal %= TABLE_SIZE;
}
return hashVal;
}
// Prints out hash table
void printTable() {
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashTable[i] == NULL) {
printf("\t%d\t---\n", i);
}
else if (hashTable[i] == DELETED_NODE) {
printf("\t%d\t---<deleted>---\n", i);
}
else {
printf("\t%d\t%s\n", i, hashTable[i]->name);
}
}
printf("End\n");
}
// Adds person to hash table
bool insert(person *p) {
if (p == NULL) return false;
unsigned int index = hash(p->name);
for (int i = 0; i < TABLE_SIZE; i++) { // Linear probing
int try = (i + index) % TABLE_SIZE;
if (hashTable[try] == NULL || hashTable[try] == DELETED_NODE) {
hashTable[try] = p;
return true;
}
}
return false;
}
// Finds specified person in hash table
person *lookup(char *name) {
int len = strlen(name);
unsigned int index = hash(name);
for (int i = 0; i < TABLE_SIZE; i++) {
int try = (i + index) % TABLE_SIZE;
if (hashTable[try] == NULL) return NULL; // not here
else if (hashTable[try] == DELETED_NODE) continue;
else if (hashTable[try] != NULL && (strncmp(name, hashTable[try]->name, len) == 0)) {
return hashTable[try];
}
}
return NULL;
}
// Removes specified person from hash table
person *delete(char *name) {
int len = strlen(name);
unsigned int index = hash(name);
for (int i = 0; i < TABLE_SIZE; i++) {
int try = (i + index) % TABLE_SIZE;
if (hashTable[try] == NULL) return NULL; // not here
else if (hashTable[try] == DELETED_NODE) continue;
else if (hashTable[try] != NULL && (strncmp(name, hashTable[try]->name, len) == 0)) {
person *tmp = hashTable[try];
hashTable[try] = DELETED_NODE;
return tmp;
}
}
return NULL;
}
// MAIN
int main() {
// Initializes empty hash table
initHashTable();
printTable();
// Initializes people to add to hash table
person jacob = {.name = "Jacob", .age = 256};
person kate = {.name = "Kate", .age = 27};
person mpho = {.name = "Mpho", .age = 14};
person harris = {.name = "Harris", .age = 18};
person sarah = {.name = "Sarah", .age = 54};
person edna = {.name = "Enda", .age = 15};
person maren = {.name = "Maren", .age = 25};
person eliza = {.name = "Eliza", .age = 34};
person robert = {.name = "Robert", .age = 1};
person jane = {.name = "Jane", .age = 75};
// Tests insert function
insert(&jacob);
insert(&kate);
insert(&mpho);
insert(&harris);
insert(&sarah);
insert(&edna);
insert(&maren);
insert(&eliza);
insert(&robert);
insert(&jane);
printTable();
// Tests lookup function
person *tmp = lookup("Harris");
if (tmp == NULL) {
printf("Not found!\n");
}
else {
printf("Found %s.\n", tmp->name);
}
tmp = lookup("12345");
if (tmp == NULL) {
printf("Not found!\n");
}
else {
printf("Found %s.\n", tmp->name);
}
// Test remove function
delete("Mpho");
printTable();
return 0;
}