-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable_linear.c
More file actions
225 lines (200 loc) · 6.83 KB
/
Copy pathhashtable_linear.c
File metadata and controls
225 lines (200 loc) · 6.83 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define size 10
//100,000,000
struct node{
int key;
char value[10];
};
typedef struct node node;
int hash(int key){
return (key % size);
}
void find_linear_search(int key, node *hashtable){
clock_t begin = clock();
for(int i=0; i<size; i++){
if(hashtable[i].key==key){
printf("Key/Value pair in index %d\n",i);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
return;
}
}
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;
}
void find(int key, node *hashtable){
clock_t begin = clock();
int index = hash(key);
if(hashtable[index].key == key){ // se tiver sido guardado sem colision
printf("Key/Value pair in index %d\n",index);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
}
else{
for(int i=index, k=0; i<size; i++){ // se tiver sido guardado com colision
if(hashtable[i].key == key){
printf("Key/Value pair in index %d\n",i);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
return;
}
else if(i==size-1){
i=-1; //after loop it will end up as 0 and so it can check from the begining of the table
}
else{
k++;
}
if(k==size-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;
}
}
}
}
void fill_table(node *hashtable){
char filled[10] = "FILLED";
for(int i=99000000; i<99999995; i++){
strcpy(hashtable[i].value, filled);
}
}
int insert(int key, char *value, node *hashtable){
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);
printf("\nSaved at index->%d\n",index);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
}
else{ //colision
printf("Colision\n");
for(int i=index, k=0; i<size; i++){
if(hashtable[i].value[0] == '\0'){
hashtable[i].key = key;
strcpy(hashtable[i].value, value);
printf("\nSaved at index->%d\n",i);
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
break;
}
else if(i==size-1){
i=-1; //after loop it will end up as 0 and so it can check from the begining of the table
}
else{
k++;
}
if(k==size-1){
printf("Hastable is full\n");
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent-> %f\n",time_spent);
return 0;
}
}
}
}
void initialize_hashtable(node *hashtable){
for(int i=0; i<size; i++){
hashtable[i].key = 0;
hashtable[i].value[0] = '\0';
}
}
void show(node *hashtable){
printf("\nIndex | Key | Value\n");
for(int i=0; i<size; i++){
printf(" %d |%5.1d | %s\n",i,hashtable[i].key, hashtable[i].value);
}
}
void main(){
static node hashtable[size];
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{
printf("\nInsert value (MAX 9 CHARS): ");
fgets(input,10,stdin);
input[strcspn(input, "\n")] = 0; //remove trailing \n
insert(key, input, hashtable);
checking_input = 0;
}
}
break;
case '2':
break;
case '3':
show(hashtable);
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, hashtable);
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);
break;
case '6':
printf("Bye");
exit(1);
break;
}
}
}