-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashlist.c
More file actions
268 lines (201 loc) · 3.98 KB
/
hashlist.c
File metadata and controls
268 lines (201 loc) · 3.98 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
261
262
263
264
265
266
267
268
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
///STRUCTS
typedef struct node_s
{
int key;
struct node_s *next;
}node;
typedef struct hash_table_s
{
int size;
node **table;
}hash_table;
///FUNCTIONS
unsigned int Hashing(int key);
void List(int key);
int Initialize(int size);
node *Search(int key);
int Insert(int key);
int Delete(int key);
int Destroy();
void WriteFile(char *filename, double Alpha, int num_op, double ttime);
///GLOBAL VARIABLES
hash_table *hash;
int HASH_SIZE = 761;
///MAIN
void main()
{
int check;
int num_op = 1000;
int max_op = 300000;
int op;
int i;
int key;
time_t t;
clock_t start, end;
double ttime = 0;
srand((unsigned) time(&t));
//printf("check = %d and size %d\n", check, hash->size);
while(num_op<max_op)
{
Initialize(HASH_SIZE);
printf("num_op: %d\n", num_op);
ttime = 0;
start = clock();
for(i = 0;i<num_op;i++)
{
op = rand()%4+1;
key = rand();
if(op<4)
Insert(key);
else if(op == 4)
Delete(key);
}
end = clock();
ttime = (double)(end-start)/CLOCKS_PER_SEC;
//printf("Time taken for %d operations: %.2f\n", num_op, ttime);
WriteFile("list.txt",(num_op/HASH_SIZE), num_op, ttime);
//List(Hashing(value));
Destroy();
num_op += 1000;
}
}
///FUNCTIONS
unsigned int Hashing(int key)
{
return key%hash->size;
}
int Initialize(int size)
{
int i;
if(size < 1)
return 2;
if((hash = malloc(sizeof(hash_table))) == NULL)
return 1;
if( (hash->table = malloc(sizeof(node) *size) ) == NULL)
return 1;
for(i = 0;i<size;i++)
{
hash->table[i] = NULL;
}
hash->size = size;
return 0;
}
node *Search(int key)
{
node *found;
unsigned int hval = Hashing(key);
found = hash->table[hval];
if(found == NULL)
return NULL;
while(found != NULL && found->key != key)
found = found->next;
return found;
}
int Insert(int key)
{
node *found;
node *new_node;
found = Search(key);
unsigned int hval = Hashing(key);
if(found != NULL)
return 2;
if((new_node = malloc(sizeof( node)) ) == NULL)
return 2;
new_node->key = key;
new_node->next = hash->table[hval];
hash->table[hval] = new_node;
return 0;
}
int Delete(int key)
{
node *found;
node *current;
node *previous = NULL;
found = Search(key);
if(found == NULL)
return 1;
unsigned int hval = Hashing(key);
current = hash->table[hval];
//Se il primo della lista e lista vuota
if((current->key == key) && (current->next == NULL))
{
hash->table[hval] = NULL;
free(current);
return 0;
} //Se primo della lista e lista non vuota
else if((current->key == key) && (current->next != NULL))
{
//printf("%d first not last\n", current->key);
hash->table[hval] = current->next;
current = NULL;
free(current);
return 0;
}
previous = current;
current = current->next;
for(current;current != NULL; current = current->next)
{
if(current->key == key)
{
//printf("%d found\n", current->key);
//printf("%d previous is\n", previous->key);
previous->next = current->next;
current = NULL;
free(current);
return 0;
}
previous = current;
//printf("%d current\n", current->key);
//printf("%d previous\n", previous->key);
}
return 2;
}
void List(int key)
{
unsigned int hval = Hashing(key);
node *nodo = hash->table[hval];
if(nodo == NULL)
{
printf("nothing in here..\n");
return;
}
while(nodo != NULL)
{
printf("%d->", nodo->key);
nodo = nodo->next;
}
printf("NULL\n");
}
int Destroy()
{
int i;
node *nodo;
node *temp;
if(hash == NULL)
return 2;
for(i = 0;i<hash->size;i++)
{
nodo = hash->table[i];
while(nodo != NULL)
{
temp = nodo;
nodo = nodo->next;
//printf("deleting %d key\n", temp->key);
free(temp);
}
}
free(hash->table);
free(hash);
return 0;
}
void WriteFile(char *filename, double Alpha, int num_op, double ttime)
{
FILE *fp;
fp = fopen(filename, "a+");
fprintf(fp,"%.2f;%d;%.2f\n", Alpha, num_op, ttime);
fclose(fp);
}