-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhash.c
More file actions
63 lines (60 loc) · 1.08 KB
/
hash.c
File metadata and controls
63 lines (60 loc) · 1.08 KB
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
#include "hash.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//GETTERS
int getHashValue(List* p){
return p->value;
}
char* getHashKey(List* p){
return p->key;
}
//HASH FUNC
unsigned int hash(char* str){
unsigned int h;
unsigned char *p;
h = 0;
for(p = (unsigned char*)str; *p != '\0'; p++){
h = MULTIPLIER * h + *p;
}
return h % HASH_SIZE;
}
//Initialize table
void init_table(){
int i = 0;
for(i; i < HASH_SIZE; i++){
table[i] = NULL;
}
}
//Print Hash table
void display() {
int i = 0;
for(i = 0; i < HASH_SIZE; i++){
if (table[i] != NULL) {
printf("[%s, %d]\n", table[i]->key, table[i]->value);
}
}
}
//Search
List* lookup(char* s){
int index = hash(s);
while (table[index] != NULL) {
if (table[index]->key) {
return table[index];
}
index ++;
index %= HASH_SIZE;
}
return NULL;
}
//Insert
void insert(char* k, int val){
int index;
List* new = (List*)malloc(sizeof(struct List));
index = hash(k);
new->key = k;
new->value = val;
new->next = table[index];
table[index] = new;
}