forked from MMohsinM/Phonebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashsep.c
513 lines (448 loc) · 11.3 KB
/
hashsep.c
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include "fatal.h"
#include "hashsep.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MinTableSize (10)
#define constant 25
struct ListNode
{
unsigned long int mobile;
unsigned long int home;
char first_name[constant];
char last_name[constant];
char company_name[constant];
Position Next;
};
typedef Position List;
/* List *TheLists will be an array of lists, allocated later */
/* The lists use headers (for simplicity), */
/* though this wastes space */
struct HashTbl
{
int TableSize;
List *TheLists;
int Elements_no; // keeps contacts number
};
/* Return next prime; assume N >= 10 */
static int
NextPrime( int N )
{
int i;
if( N % 2 == 0 )
N++;
for( ; ; N += 2 )
{
for( i = 3; i * i <= N; i += 2 )
if( N % i == 0 )
goto ContOuter; /* Sorry about this! */
return N;
ContOuter: ;
}
}
/* Hash function for ints */
Index
Hash(const char *Key, int TableSize)
{
unsigned int HashVal = 0;
while (*Key != '\0') // sum of ascii values of chars mod tablesize
HashVal += *Key++;
return HashVal % TableSize;
}
/* START: fig5_8.txt */
HashTable
InitializeTable( int TableSize )
{
HashTable H;
int i;
/* 1*/ if( TableSize < MinTableSize )
{
/* 2*/ Error( "Table size too small" );
/* 3*/ return NULL;
}
/* Allocate table */
/* 4*/ H = malloc( sizeof( struct HashTbl ) );
/* 5*/ if( H == NULL )
/* 6*/ FatalError( "Out of space!!!" );
/* 7*/ H->TableSize = NextPrime( TableSize );
/* Allocate array of lists */
/* 8*/ H->TheLists = malloc( sizeof( List ) * H->TableSize );
/* 9*/ if( H->TheLists == NULL )
/*10*/ FatalError( "Out of space!!!" );
/* Allocate list headers */
/*11*/ for( i = 0; i < H->TableSize; i++ )
{
/*12*/ H->TheLists[ i ] = malloc( sizeof( struct ListNode ) );
/*13*/ if( H->TheLists[ i ] == NULL )
/*14*/ FatalError( "Out of space!!!" );
else
/*15*/ H->TheLists[ i ]->Next = NULL;
}
H->Elements_no = 0;
/*16*/ return H;
}
/* END */
// FUNCTION FIND
// DESCRIPTION
// Returns pointer to contact based on first name and
// second name
Position Find(ElementType* fn, ElementType* sn, HashTable H)
{
Position P;
List L;
int check = 0;
L = H->TheLists[Hash(fn, H->TableSize)];
P = L->Next;
while (P != NULL)
{
if (strcmp(P->first_name, fn) == 0)
if (strcmp(P->last_name, sn) == 0)
return P;
P = P->Next;
}
return P;
}
/* START: fig5_9.txt */
Position
Retrieve( ElementType* key, HashTable H ,char mode)///Retrieve Function
{
Position P;
List L;
int check = 0;//counts matched queries
/* 1*/ L = H->TheLists[ Hash( key, H->TableSize ) ];
/* 2*/ P = L->Next;
char found = 'j';
/* 3*/ while( P != NULL)
{
if (strcmp(P->first_name, key) == 0) {
if (!check)
{
// printf("First Name\tLast Name\tMobile\t\tHome\t\tCompany\n");
}
printf("%s\t%s\t",P->first_name ,P->last_name);
printf("0%u\t0%u\t%s\n", P->mobile, P->home, P->company_name);
check++;
if (mode != 'r')
{
printf("Is this your required contact (y to stop): ");
scanf_s("%c", &found,1);
getchar();
if (found == 'y' || found == 'Y')
break;
}
}
P = P->Next;
}
if (!check)
puts("No matched contacts");
/* Probably need strcmp!! */
/* 5*/ return P;
}
void
Insert( Position Input, HashTable H ) /// Inserts contacts into the hashtable
{
Position NewCell = NULL;
Position Pos = NULL;
List L;
//_strlwr_s(Input->first_name = strdup(Input->first_name), strlen(Input->first_name));
/* 1*/ Pos = Find( Input->first_name,Input->last_name, H );
/* 2*/ if( Pos == NULL ) /* Key is not found */
//{
/* 3*/ NewCell = malloc( sizeof( struct ListNode ) );
/* 4*/ if( NewCell == NULL )
/* 5*/ FatalError( "Out of space!!!" );
else
{
/* 6*/ L = H->TheLists[ Hash(Input->first_name, H->TableSize ) ];
/* 7*/ NewCell->Next = L->Next;
/* 8*/ strcpy_s(NewCell->first_name, constant, Input->first_name);
strcpy_s(NewCell->last_name, constant, Input->last_name);
strcpy_s(NewCell->company_name, constant, Input->company_name);
NewCell->mobile = Input->mobile;
NewCell->home = Input->home;
/* 9*/ L->Next = NewCell;
}
// }
H->Elements_no +=1;
}
/* END */
/*ElementType
Retrieve( Position P )
{
return P->Element;
}
*/
void
DestroyTable( HashTable H )
{
int i;
for( i = 0; i < H->TableSize; i++ )
{
Position P = H->TheLists[ i ];
Position Tmp;
while( P != NULL )
{
Tmp = P->Next;
free( P );
P = Tmp;
}
}
free( H->TheLists );
free( H );
}
Position FindPrevious(Position P, List L)
{
Position Prev;
Prev = L;
while (Prev->Next != NULL && Prev->Next != P)
Prev = Prev->Next;
return Prev;
}
void Delete(ElementType* fname,HashTable H) {// update this
Position P =Retrieve(fname, H,'d');
if (P == NULL) {
puts("Contact not found");
}
else
{
Position Prev = FindPrevious(P, H->TheLists[Hash(P->first_name, H->TableSize)]);
Prev -> Next = P -> Next;
free(P);
puts("Delete Successful");
}
}
void update(ElementType* fname, HashTable H)
{
Position P = Retrieve(fname, H, 'd');
char num[constant];
if (P == NULL)
{
puts("Contact not found");
}
else
{
int choice;
struct ListNode f;
clock_t start, end;
double runtime;
puts("what to edit\n1. mobile\n2.home\n3.work\n4. First Name\n5. Second Name\nAny other key to exit");
scanf_s("%d", &choice);
getchar();
if (choice == 3)
{
printf("Enter the company name\n");
scanf_s("%s", &P->company_name, constant);
getchar();
}
else if (choice == 1)
{
do
{
puts("Enter mobile number");
scanf_s("%s", num, constant);
getchar();
if ( hasAlphabets(num)) {
puts("Invalid Number");
}
else
break;
} while (1);
P->mobile = atoi(num);
}
else if (choice == 2)
{
do
{
puts("Enter home number");
scanf_s("%s", num,constant);
getchar();
if (hasAlphabets(num))
puts("Invalid Number");
else
break;
} while (1);
P->home = atoi(num);
}
else if (choice == 4)
{
f = *P;
Position Prev = FindPrevious(P, H->TheLists[Hash(P->first_name, H->TableSize)]);
Prev->Next = P->Next;
free(P);
printf("Enter first name\n");
scanf_s("%s", &f.first_name, constant);
getchar();
Insert(&f, H);
P = &f;
}
else if (choice == 5)
{
printf("Enter second name\n");
scanf_s("%s", &P->last_name, constant);
getchar();
}
printf("First name = %s\n", P->first_name);
printf("Last Name = %s\n", P->last_name);
printf("Company name = %s\n", P->company_name);
printf("Mobile = 0%u\n", P->mobile);
printf("Home = 0%u\n", P->home);
}
}
// Sorts the table in descending order
void Sort(HashTable H) {
int i;
int j = 0;
SearchTree T = MakeEmpty(NULL);
for (i = 0; i < H->TableSize; i++)
{
Position P = H->TheLists[i]->Next;
while (P != NULL)
{
T= TreeInsert(P, T);
P = P->Next;
}
j = 0;
}
puts("First Name\tSecond Name\tMoblile Number\tPhone Number\tWork");
PrintAscend(T);
}
//for file reading
void Load(char *path,HashTable H)
{
FILE *fptr;
char line[150]; //for lines in file
const char delimeter[3] = ","; // delimiter for fields in file
char *token = NULL;
char *next_token = NULL;
//give path for the file
fopen_s(&fptr, path, "r");
//check if file is opening successfully
if (fptr == NULL)
{
perror("error ");
exit(1);
}
else
printf("success!\n");
///////My variables////
int term;
Position f = malloc(sizeof(struct ListNode));
//read data from file
while (!feof(fptr))
{
if (fgets(line, 150, fptr) != NULL)
{
term = 1;
char *ptr;
//separate data fields using delimiter
token = strtok_s(line, delimeter, &next_token);
strcpy_s(f->first_name,constant,token);
// walk through other tokens
while (token != NULL) {
token = strtok_s(NULL, delimeter, &next_token);
term++;
if (token == NULL)
break;
if (term % 4 == 0)
{
f->mobile = strtoul(token,&ptr,10);
}
else if (term % 2 == 0)
{
strcpy_s(f->last_name, constant, token);
}
else if (term % 3 == 0)
{
strcpy_s(f->company_name, constant, token);
}
else if (term % 5 == 0)
{
f->home = strtol(token,&ptr,10);
}
}
Insert(f, H);
}
}
fclose(fptr); // close file
}
//writing data to hard disk in a file
void write(char *path,HashTable H) {
FILE *fptr;
fopen_s(&fptr, path, "w");
if (fptr == NULL)
Error("Cannot open file");
int i = 0;
List L;
Position P;
for (i = 0; i < H->TableSize; i++) {
L = H->TheLists[i];
P = L->Next;
while (P != NULL) {
fprintf(fptr, "%s,", P->first_name);
fprintf(fptr, "%s,", P->last_name);
fprintf(fptr, "%s,",P->company_name);
fprintf(fptr, "%u,%d\n", P->mobile, P->home);
P = P->Next;
}
}
fclose(fptr);
}
HashTable Rehash(HashTable H)
{
int i, OldSize;
List* OldLists;
Position P;
OldLists = H->TheLists;
OldSize = H->TableSize;
H = InitializeTable(2 * OldSize);
for (i = 0; i < OldSize; i++)
{
P = OldLists[i]->Next;
while (P != NULL)
{
Insert(P, H);
P = P->Next;
}
}
free(OldLists);
return H;
}
int TableElements(HashTable H)
{
return H->Elements_no;
}
int TableSize(HashTable H)
{
return H->TableSize;
}
// Prints The table index wise
void PrintTable(HashTable H) {
int i;
int j = 0;
for (i = 0; i < H->TableSize; i++) {
printf("Index = %d", i);
Position P = H->TheLists[i]->Next;
while (P != NULL) {
printf(" Value %d = %s", j++, P->first_name);
P = P->Next;
}
if (P != NULL)
printf("%s", "Empty");
puts("");
j = 0;
}
}
//hasAlphabets is used to check whether a string has alphabets in it or not
int hasAlphabets(ElementType* x)
{
int len = strlen(x);
for (int i = 0; i < len; i++)
{
if (*x<'0' || *x>'9')
{
return 1;
}
x++;
}
return 0;
}