-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable_linked_list.c
More file actions
260 lines (227 loc) · 7.63 KB
/
hashtable_linked_list.c
File metadata and controls
260 lines (227 loc) · 7.63 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define size 10
//10,000,000
struct node{
int key;
char value[10];
struct node *next;
};
typedef struct node node;
static node hashtable[size];
int hash(int key){
return (key % size);
}
void find_linear_search(int key, node *hashtable, int verbose){
int node_number=0;
clock_t begin = clock();
for(int i=0; i<size; i++){
node *on = &hashtable[i];
while(1){
if(on->key == key){
if(verbose==1){
printf("Key/Value pair in index %d, node number %d\n",i, node_number);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
}
return;
}
if(on->next == NULL){
break;
}
else{
on = on->next;
node_number++;
}
}
}
printf("Key %d not in hashtable\n", key);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time Spent-> %f\n",time_spent);
return;
}
int find(int key, int verbose){ // returns 1 if found & zero if not in table
clock_t begin = clock();
int node_number=0;
int index = hash(key);
node *on = &hashtable[index];
while(1){
if(on->key == key){
if(verbose==1){
printf("Key/Value pair in index %d, node number %d\n",index, node_number);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
}
return 1;
}
else if(on->next != NULL){
on = on->next;
node_number++;
}
else{
if(verbose==1){
printf("Key %d not in hashtable\n", key);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
}
return 0;
}
}
}
void fill_table(node *hashtable){
char filled[10] = "FILLED";
for(int i=99000000; i<99999995; i++){
strcpy(hashtable[i].value, filled);
}
}
void insert(int key, char *value){ //double pointer to change values
clock_t begin = clock();
int index;
index = hash(key);
if(hashtable[index].value[0] == '\0'){ //no colision
printf("No Colision\n");
hashtable[index].key = key;
strcpy(hashtable[index].value, value);
hashtable[index].next = NULL;
printf("\nSaved at index->%d Node->0\n",index);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
}
else{ //colision
int node_index = 1;
node *on = &hashtable[index]; // on only starts at the on and then walks trough the LL
// (points to the next node after the first)
node *head = &hashtable[index];
node *new = malloc(sizeof(node));
new->key = key;
strcpy(new->value, value);
new->next = NULL;
if(on->next == NULL){
on->next = new;
printf("Colision\n");
printf("\nSaved at index->%d Node->1\n",index);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
return;
}
while(on->next != NULL){
on = on->next;
node_index++;
}
on->next = new;
printf("Colision\n");
printf("\nSaved at index->%d Node->%d\n",index,node_index);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
}
}
void initialize_hashtable(){
for(int i=0; i<size; i++){
hashtable[i].key = 0;
hashtable[i].value[0] = '\0';
hashtable[i].next = NULL;
}
}
void show(){
for(int i=0; i<size; i++){
node *on = hashtable[i].next; // on only starts as the on and then walks trough the LL
if(on == NULL){
printf("\n|I:%d|K:%d|V:%s| --> NULL\n",i,hashtable[i].key, hashtable[i].value);
}
else{
printf("\n|I:%d|K:%d|V:%s| --> ",i,hashtable[i].key, hashtable[i].value);
while(on != NULL){
printf("|I:%d|K:%d|V:%s| --> ",i,on->key, on->value);
on = on->next;
}
printf("NULL\n");
}
}
}
void main(){
char input[10];
int key, loop=1, checking_input=1;
initialize_hashtable(hashtable);
while(loop){
printf("\nAdd key/value(1)\nRemove key/value(2)\nSee hashtable(3)\nSearch by key(4)\nLinear search(5)\nExit(6)\n-> ");
fgets(input, 2, stdin);
while ((getchar()) != '\n'); //clear stdin buffer
switch (input[0]){
default:
printf("Incorrect input.\n");
break;
case '1':
checking_input=1;
while(checking_input){
printf("\nInsert key (MAX 49 CHARS): ");
fgets(input,50,stdin);
key = atoi(input); //convert str to int
if(key==0 && input[0] != '0'){
printf("Invalid key\n");
checking_input = 1;
}
else if(find(key, 0) == 1){
printf("Key already in hashtable\n");
}
else{
printf("\nInsert value (MAX 9 CHARS): ");
fgets(input,10,stdin);
input[strcspn(input, "\n")] = 0; //remove trailing \n
insert(key, input);
checking_input = 0;
}
}
break;
case '2':
break;
case '3':
show();
break;
case '4':
checking_input=1;
while(checking_input){
printf("\nInsert key (MAX 49 CHARS): ");
fgets(input,50,stdin);
key = atoi(input); //convert str to int
if(key==0 && input[0] != '0'){
printf("Invalid key\n");
checking_input = 1;
}
else{
checking_input=0;
}
}
find(key, 1);
break;
case '5':
checking_input=1;
while(checking_input){
printf("\nInsert key (MAX 49 CHARS): ");
fgets(input,50,stdin);
key = atoi(input); //convert str to int
if(key==0 && input[0] != '0'){
printf("Invalid key\n");
checking_input = 1;
}
else{
checking_input=0;
}
}
find_linear_search(key,hashtable,1);
break;
case '6':
printf("Bye");
exit(1);
break;
}
}
}