-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable_Chaning_Flights.cpp
175 lines (153 loc) · 2.94 KB
/
HashTable_Chaning_Flights.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Zbor
{
char* destinatie;
int pasageri;
};
struct Nod
{
Nod* next;
Zbor info;
};
struct HashTable
{
Nod* *lista;
int dim;
};
Zbor initZbor(const char* dest, int pas)
{
Zbor a;
a.destinatie = (char*)malloc(sizeof(char) * (strlen(dest) + 1));
strcpy(a.destinatie, dest);
a.pasageri = pas;
return a;
}
Nod* initNod(Nod* next, Zbor info)
{
Nod* nod = (Nod*)malloc(sizeof(Nod));
nod->info = info;
nod->next = next;
return nod;
}
HashTable initHashTable(int dim)
{
HashTable h;
h.dim = dim;
h.lista = (Nod**)malloc(sizeof(Nod) * h.dim);
for (int i = 0; i < h.dim; i++)
{
h.lista[i] = NULL;
}
return h;
}
int hashFunction(HashTable h, Zbor z)
{
int hashCode = 0;
for (int i = 0; i < strlen(z.destinatie); i++)
{
hashCode += z.destinatie[i];
}
return hashCode % h.dim;
}
Nod* inserareNod(Nod* lista, Zbor z)
{
if (lista)
{
Nod* nou = initNod(lista, z);
return nou;
}
else
{
return initNod(NULL, z);
}
}
void inserareHashTable(HashTable h, Zbor z)
{
int hashCode = hashFunction(h, z);
h.lista[hashCode] = inserareNod(h.lista[hashCode], z);
}
void afisareZbor(Zbor a)
{
printf("\nDestinatie: %s\nNumar pasageri: %d", a.destinatie, a.pasageri);
}
void afisareLista(Nod* lista)
{
Nod* temp = lista;
while (temp)
{
afisareZbor(temp->info);
temp = temp->next;
}
}
void afisareHashTable(HashTable h)
{
for (int i = 0; i < h.dim; i++)
{
printf("\n------------------Pozitia %d--------------------", i);
if (h.lista[i] != NULL)
{
afisareLista(h.lista[i]);
}
else
{
printf("\nLista este goala!");
}
}
}
Zbor extragereZbor(HashTable h, const char* destinatie)
{
int hashCode = hashFunction(h, initZbor(destinatie, 0));
Nod* lista = h.lista[hashCode];
if(lista)
{
if (strcmp(lista->info.destinatie, destinatie) == 0)
{
if (lista->next == NULL)
{
Zbor tempZbor = initZbor(lista->info.destinatie, lista->info.pasageri);
h.lista[hashCode] = NULL;
return tempZbor;
}
else
{
Zbor tempZbor = initZbor(lista->info.destinatie, lista->info.pasageri);
h.lista[hashCode] = h.lista[hashCode]->next;
free(lista);
return tempZbor;
}
}
while (lista->next && strcmp(lista->next->info.destinatie, destinatie) != 0)
{
lista = lista->next;
}
if (lista->next)
{
}
}
}
void main()
{
Zbor z1 = initZbor("Castellon", 220);
Zbor z2 = initZbor("Bucuresti", 320);
Zbor z3 = initZbor("New York", 250);
Zbor z4 = initZbor("Berlin", 230);
Zbor z5 = initZbor("Bruxelles", 150);
Zbor z6 = initZbor("Vienna", 290);
Zbor z7 = initZbor("Los Angeles", 300);
HashTable h = initHashTable(5);
inserareHashTable(h, z1);
inserareHashTable(h, z2);
inserareHashTable(h, z3);
inserareHashTable(h, z4);
inserareHashTable(h, z5);
inserareHashTable(h, z6);
inserareHashTable(h, z7);
afisareHashTable(h);
printf("\n\n");
Zbor z8 = extragereZbor(h, "Castellon");
afisareZbor(z8);
printf("\n\n");
afisareHashTable(h);
}