-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.cpp
453 lines (408 loc) · 9.07 KB
/
hashtable.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "hashtable.hpp"
using namespace std;
hnode::hnode(){
ptr=NULL;
next=NULL;
}
hnode::~hnode(){
delete ptr;
}
hplist::hplist(int pc):postcode(pc){
header=NULL;
next=NULL;
size=0;
}
hplist::~hplist(){
hnode *tmp;
hnode *tmp2;
if (header!=NULL)
{
tmp=header;
while((tmp->getnext())!=NULL){
tmp2=tmp->getnext();
delete tmp;
tmp=tmp2;
}
delete tmp;
}
}
void hplist::insert(student *st){
hnode *newNode;
hnode *tmp;
newNode=new hnode();
newNode->setstudent(st);
if (header==NULL) /*if its the first student to this post code set the header to point at him*/
{
header=newNode;
}else{
tmp=header;
while((tmp->getnext())!=NULL){ /*traverse the list until you reach the end of it*/
tmp=tmp->getnext();
}
tmp->setnext(newNode); /*then add him*/
}
size++;
}
bool hplist::deletestud(int id, int pcode){
hnode *tmp;
hnode *tmp2;
if (pcode == postcode)
{
if(header!=NULL){ /*if there are students in this postcode*/
tmp=header;
if (((tmp->getstudent())->getstudid()) == id) /*if the desired student is the first one the list*/
{
if((tmp->getnext()) == NULL){ /*if he is the only one too*/
size--;
delete tmp;
header=NULL;
return true;
}else{
header=header->getnext();
delete tmp;
size--;
return true;
}
}
tmp2=header;
tmp=tmp->getnext();
while(tmp!=NULL){ /*traverse until you find the desired student*/
if (((tmp->getstudent())->getstudid()) == id) {
tmp2->setnext(tmp->getnext());
size--;
delete tmp;
return true;
}
tmp2=tmp;
tmp=tmp->getnext();
}
}
}
return false;
}
void hplist::printlist(){
hnode *tmp;
tmp=header;
while(tmp != NULL){
(tmp->getstudent())->printall();
tmp=tmp->getnext();
}
}
float hplist::getavgpa(){
hnode *tmp;
float allgpa=0;
tmp=header;
while(tmp != NULL){
allgpa=allgpa + (tmp->getstudent())->getgpa();
tmp=tmp->getnext();
}
return (allgpa/size);
}
hlist::hlist(){
header=NULL;
size=0;
}
hlist::~hlist(){
hplist *tmp;
hplist *tmp2;
if (header!=NULL)
{
tmp=header;
while((tmp->getnext())!=NULL){
tmp2=tmp->getnext();
delete tmp;
tmp=tmp2;
}
delete tmp;
}
}
void hlist::insert(student *st){
hplist *tmp;
hplist *prevnode;
tmp=header;
if (tmp == NULL) /*if there are no hplists on this hash table cell*/
{
header=new hplist(st->getpostcode());
header->insert(st);
size++;
}else{
while(tmp != NULL){ /*traverse list*/
prevnode=tmp;
if (tmp->getpostcode() == st->getpostcode()) /*to find an hplist with the students st postcode*/
{
tmp->insert(st);
size++;
break;
}
tmp=tmp->getnext();
}
if (tmp == NULL) /*if didn't found the proper hplists create a new one*/
{
tmp=new hplist(st->getpostcode());
tmp->insert(st);
size++;
prevnode->setnext(tmp);
}
}
}
void hlist::printlist(){
hplist *tmp;
tmp=header;
while(tmp != NULL){
tmp->printlist();
tmp=tmp->getnext();
}
}
bool hlist::deletestud(int id, int postcode){
hplist *tmp;
hplist *tmp2;
hnode *tmp3;
bool flag=false;
if(header!=NULL){ /*if the list is not empty*/
tmp2=header;
tmp=header->getnext(); /*keep to the variable tmp the address of the second hplist*/
flag=header->deletestud(id,postcode); /*try deleting him from the first hplist*/
tmp3=header->getheader(); /*and keep to variable tmp3 the address of the maybe deleted hplists header*/
if (flag) /*if deleted him from the hlists header*/
{
if (tmp3 == NULL) /*and it was the only student in this post code*/
{
delete header; /*delete this hplist*/
header=tmp; /*and set hlist's header to the second hplists of the hlist*/
}
return true;
}
while(tmp != NULL && !flag){ /*else traverse*/
flag=tmp->deletestud(id, postcode); /*and try deleting him in each hplist*/
if (flag)
{
tmp3=tmp->getheader();
if (tmp3 == NULL) /*if he was the only one in this postcode*/
{
tmp2->setnext(tmp->getnext());
delete tmp; /*delete the hplists of this post code*/
}
return true;
}
tmp2=tmp;
tmp=tmp->getnext();
}
}
return false;
}
float hlist::getavgpa(int pcode){
hplist *tmp;
float avgpa=-1;
tmp=header;
while(tmp != NULL){
if (tmp->getpostcode() == pcode)
{
avgpa=tmp->getavgpa();
break;
}
tmp=tmp->getnext();
}
return avgpa;
}
void hlist::bubsort(int k, hplist *tmp){ /*typical implementation of bubble sort function*/
int n,s;
student *swap;
student **sorted;
hnode *node;
node=tmp->getheader();
s=tmp->getsize();
sorted=new student *[s];
for (int i = 0; i < s; ++i)
{
sorted[i]=node->getstudent(); /*get to a table name sorted all the students of tmp's postcode*/
node=node->getnext();
}
for (int i = 0; i < s -1; ++i)
{
for (int j = 0; j < s - i -1; ++j)
{
if (sorted[j]->getgpa() < sorted[j+1]->getgpa()) /*sort them*/
{
swap=sorted[j];
sorted[j]=sorted[j+1];
sorted[j+1]=swap;
}
}
}
if (k <= s) /*if students asked less than the table's size*/
{
for (int i = 0; i < k; ++i)
{
sorted[i]->printall(); /*print the amoun of top students asked*/
}
}else{
cerr<<"Asked for more students than the available. All available will be printed."<<endl;
for (int i = 0; i < s; ++i) /*print all the available students if this post code*/
{
sorted[i]->printall();
}
}
delete sorted;
}
void hlist::taverage(int k, int pcode){
hplist *tmp;
student **sorted;
tmp=header;
while(tmp != NULL){ /*traverse throught the hlist to find the desirable hplist*/
if (tmp->getpostcode() == pcode)
{
bubsort(k,tmp);
break;
}
tmp=tmp->getnext();
}
if (tmp == NULL) /*if reached the end of the hlist without founding the postcode*/
{
cerr<<"No students found on this postcode/postcode not found."<<endl;
}
}
void hlist::ct(int pcode,char *dpt){
hplist *tmp;
hnode *node;
bool flag=false;
tmp=header;
while(tmp != NULL){ /*traverse through hlist*/
if (tmp->getpostcode() == pcode) /*if found the proper postcode*/
{
node=tmp->getheader();
while(node != NULL){ /*traverse through hplists*/
if (!strcmp((node->getstudent())->getdeprt(),dpt)) /*if a student belong to the desired department*/
{
(node->getstudent())->printall(); /*print him*/
flag=true;
}
node=node->getnext();
}
break;
}
tmp=tmp->getnext();
}
if (tmp == NULL || !flag) /*if reached the end of the hlist and not found the postcode or didn't found*/
/*any students of this department in this postcode*/
{
cerr<<"No students found on this "<<endl;
cerr<<"postcode/department with this postcode or postcode not found."<<endl;
}
}
float hlist::pcodesize(int pcode){
hplist *tmp;
tmp=header;
while(tmp != NULL){ /*traverse through hlist*/
if (tmp->getpostcode() == pcode)
{
return (float)tmp->getsize();
}
tmp=tmp->getnext();
}
if (tmp == NULL) /*if not fount this postcode*/
{
cerr<<"No students found on this postcode/postcode not found."<<endl;
}
return 0;
}
void hlist::pe(float allsize){
hplist *tmp;
tmp=header;
while(tmp != NULL){
printf("The postcode %d hosts the %2.2f %% of the students.\n",tmp->getpostcode(), (tmp->getsize()*100)/allsize);
tmp=tmp->getnext();
}
}
htable::htable(int entr):entries(entr){
lists=new hlist *[entries];
for (int i = 0; i < entries; ++i)
{
lists[i]=new hlist();
}
}
htable::~htable(){
for (int i = 0; i < entries; ++i)
{
delete lists[i];
}
cout<<"Goodbye."<<endl;
delete [] lists;
}
int htable::hashfunc(int postcode){ /*function to hash the postcodes*/
return (postcode % entries);
}
void htable::insert(student *st){
int position;
position=hashfunc(st->getpostcode()); /*decide with the help of hash func in which cell to insert*/
lists[position]->insert(st);
size++;
}
void htable::printall(){
for (int i = 0; i < entries; ++i)
{
if (lists[i] != NULL)
{
lists[i]->printlist();
}
}
}
bool htable::deletestud(int id, int postcode){
int position;
bool flag;
position=hashfunc(postcode);
flag=lists[position]->deletestud(id, postcode);
if (flag) /*if deleted with success decrease the size*/
{
size--;
}
}
float htable::average(int pcode){
hlist *tmp;
int position;
position=hashfunc(pcode);
tmp=lists[position];
return tmp->getavgpa(pcode);
}
void htable::taverage(int k, int pcode){
hlist *tmp;
int position;
position=hashfunc(pcode);
tmp=lists[position];
tmp->taverage(k,pcode);
}
void htable::ct(int pcode,char *dpt){
hlist *tmp;
int position;
position=hashfunc(pcode);
tmp=lists[position];
tmp->ct(pcode,dpt);
}
void htable::p(int pcode, float allsize){
if (allsize) /*if the size is not zero*/
{
hlist *tmp;
float pcodesize;
int position;
position=hashfunc(pcode);
tmp=lists[position];
pcodesize=tmp->pcodesize(pcode);
pcodesize=pcodesize*100;
pcodesize=pcodesize/allsize;
printf("In this postcode lives the %2.2f %% of students.\n",pcodesize );
}else{
cerr<<"Cannot find statistics with zero inputs."<<endl;
}
}
void htable::pe(float allsize){
if (allsize) /*if the size is not zero*/
{
hlist *tmp;
for (int i = 0; i < entries; ++i)
{
tmp=lists[i];
tmp->pe(allsize);
}
}else{
cerr<<"Cannot find statistics with zero inputs."<<endl;
}
}