-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_chain.cpp
95 lines (86 loc) · 2.42 KB
/
hash_chain.cpp
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
#include "utils.cpp"
struct nodLista {
Articol info;
nodLista* next;
};
int hash(int id, float marime, int hashSize) {
int hashValue = int(round(id * marime)) % hashSize;
return hashValue;
}
void adaugaArticolC(nodLista** hashC, int hashSize, Articol articol) {
int hashValue = hash(articol.id, articol.marime, hashSize);
nodLista* nou = (nodLista*)malloc(sizeof(nodLista));
nodLista* curent;
printf("%03d --> ",hashValue);
afiseazaArticol(articol);
nou->info = articol;
nou->next = NULL;
// nu avem cap
if (!hashC[hashValue]) {
hashC[hashValue] = nou;
} else {
// avem cap
curent = hashC[hashValue];
int link = 0;
do {
printf("Coliziune pe pozitia %d:%d pentru articolul cu id %d!\n", hashValue, link, articol.id);
if (curent->next) {
curent = curent->next;
link++;
} else break;
} while(true);
curent->next = nou;
}
}
void citesteArticoleC(nodLista** hashC, char* f_name, int hashSize, int items) {
// citire din fisier
FILE* f = fopen(f_name,"r");
Articol articol;
for (int i=0; i<items; i++) {
articol = citesteArticol(f);
adaugaArticolC(hashC, hashSize, articol);
}
fclose(f);
}
Articol * cautaArticol(nodLista** hashC, int hashSize, int id, float marime) {
int hashValue = hash(id, marime, hashSize);
nodLista* curent = hashC[hashValue];
int link = 0;
while (curent) {
if (curent->info.id != id) {
printf("Coliziune pe pozitia %d:%d pentru articolul cu id %d!\n",hashValue, link, id);
curent = curent->next;
link++;
} else break;
}
if (curent == NULL) return NULL;
return &curent->info;
}
int main() {
int hashSize = 4;
// lista articole
char f_name[] = "data-structures.in";
// citire numar de linii din fisier
int items = fileRecords(f_name);
nodLista** hashC = (nodLista**)malloc(sizeof(nodLista*)*hashSize);
for (int i=0; i<hashSize;i++)hashC[i] = NULL;
citesteArticoleC(hashC, f_name, hashSize, items);
// cautare hash nodListaing
while(true) {
printf("Introduceti id-ul si marimea de cautat (2 4.83) :");
int id = 0;
float marime = NULL;
scanf("%d %f", &id, &marime);
// iesire daca id = 0
if (id == 0) break;
Articol * p_articol = cautaArticol(hashC, hashSize, id, marime);
Articol articol;
if (p_articol) {
articol = * p_articol;
afiseazaArticol(articol);
} else {
printf("Id-ul nu a fost gasit!\n");
}
}
return 0;
}