forked from embedded2015/phonebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonebook_opt.c
47 lines (42 loc) · 917 Bytes
/
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
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "phonebook_opt.h"
/* FILL YOUR OWN IMPLEMENTATION HERE! */
entry *hashTable[SIZE];
int hashFunction(char lastName[])
{
int index=0;
while(*lastName){
index += *lastName++;
}
return (index%SIZE);
}
entry *findName(char lastName[], entry *pHead)
{
/* TODO: implement */
int key = hashFunction(lastName);
pHead = hashTable[key];
while(pHead){
if(!strcasecmp(lastName, pHead->lastName))
return pHead;
pHead = pHead->pNext;
}
return NULL;
}
entry *append(char lastName[], entry *e)
{
int key = hashFunction(lastName);
if(e)e = (entry *)malloc(sizeof(entry));
if(hashTable[key]){
e->pNext = hashTable[key];
strcpy(e->lastName, lastName);
hashTable[key] = e;
}else{
strcpy(e->lastName,lastName);
hashTable[key] = e;
e->pNext = NULL;
}
return e;
}