forked from sysprog21/phonebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonebook_opt.c
73 lines (63 loc) · 1.37 KB
/
phonebook_opt.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
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "phonebook_opt.h"
unsigned long hash(char *str)
{
unsigned long hash = 0;
//DJB2Hash
/*
hash = 5381;
int c = 0;
while (c = *str++)
hash = ((hash << 5) + hash) + c; // hash * 33 + c
return hash;
*/
//BKDRHash
/*
unsigned int seed = 31; //SDBHash set seed as 65599
while (*str) {
hash = hash * seed + (*str++);
}
return hash;
*/
/*
//RSHHash
unsigned int magic = 63689;
int c = 0;
while (c = *str++)
{
hash = hash * magic + c;
magic *= 378551;
}
*/
//APHash
int c = 0;
long i = 0;
for (i = 0; c = *str++; i++) {
if((i & 1) == 0) {
hash^= ((hash << 7) ^ c ^ (hash >> 3));
} else {
hash ^= (~((hash << 11) ^ c ^ (hash >> 5)));
}
}
return hash;
}
entry *findName(char lastName[], entry *pHead)
{
while (pHead != NULL) {
if (strcasecmp(lastName, pHead->lastName) == 0)
return pHead;
pHead = pHead->pNext;
}
return NULL;
}
entry *append(char lastName[], entry *e)
{
e->pNext = (entry *) malloc(sizeof(entry));
e = e->pNext;
strcpy(e->lastName, lastName);
e->pNext = NULL;
return e;
}