-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.c
More file actions
143 lines (134 loc) · 3.77 KB
/
Copy pathhash.c
File metadata and controls
143 lines (134 loc) · 3.77 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
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
#include "hash.h"
struct Hash *createHash(size_t size){
struct Hash *hash = malloc(sizeof(struct Hash));
if (hash == NULL)
return NULL;
hash->size = size;
hash->entries = calloc(size, sizeof(struct Entry*));
if (hash->entries == NULL){
printf("Calloc failed. No memory for hash.\n");
return NULL;
}
return hash;
}
int destroyHash(struct Hash *hash){
struct Entry *p;
struct Entry *pPrev;
for (size_t i = 0; i < hash->size; i++){
pPrev = *(hash->entries + i);
if (pPrev == NULL){
free(pPrev);
continue;
}
p = pPrev->next;
while (p != NULL){
free(pPrev);
pPrev = p;
p = p->next;
}
free(pPrev);
free(p);
}
free(hash->entries);
free(hash);
return 0;
}
int hashFunction(char *key, struct Hash *hash){
/*the index is generated by creating a large number from the characters
in the string. It does this by using the fact that a char is 8 bits
long.*/
unsigned int index = 0;
uint64_t stringbit = 0L;
while(*key != '\0'){
stringbit |= *key;
key++;
stringbit <<= 8;
}
index = stringbit % hash->size;
return index;
}
int insertEntry(char *key, char *value, struct Hash *hash){
int index = hashFunction(key, hash);
struct Entry *p = *(hash->entries + index);
if (p == NULL){//no collision
p = malloc(sizeof(struct Entry));
p->key = key;
p->value = value;
p->next = NULL;
(*(hash->entries + index)) = p;
}
else{//collision
while(p->next != NULL){//steps to the end of the linked list
if (strcmp(p->key, key) == 0){
printf("Key already exists. Nothing changed.\n");
return 1;
}
p = p->next;
}
if (strcmp(p->key, key) == 0){
printf("Key already exists. Nothing changed.\n");
return 1;
}
//inserts entery
p->next = malloc(sizeof(struct Entry));
p = p->next;
p->key = key;
p->value = value;
}
return 0;
}
int deleteEntry(char *key, struct Hash *hash){
int index = hashFunction(key, hash);
struct Entry *p = *(hash->entries + index);
if (p == NULL){ //no value at index
printf("No entry by that key\n");
return 1;
}
else if (strcmp(p->key, key) == 0){//no colission
*(hash->entries + index) = p->next; //shifts linked list back one
free(p);
return 0;
}
else{ //collision
struct Entry *pPrev = *(hash->entries + index);
p = p->next;
while (p != NULL){
if (strcmp(p->key, key) == 0){
pPrev->next = p->next; //relinks previous linked list node
free(p);
return 0;
}
p = p->next;
}
}
printf("No entry by that key\n");
return 0;
}
char *lookUp(char *key, struct Hash *hash){
int index = hashFunction(key, hash);
struct Entry *p = *(hash->entries + index);
if (p == NULL) //no value at index
return "No entry by that key\n";
else if (strcmp(p->key, key) == 0)//no colission
return p->value;
else{ //collision
while (p != NULL){
if (strcmp(p->key, key) == 0)
return p->value;
p = p->next;
}
}
return "No entry by that key\n";
}
int printTable(struct Hash *hash){
//for and while loop step through entire data stucture.
for (int i = 0; i < hash->size; i++){
struct Entry *p = *(hash->entries + i);
while(p != NULL){
printf("[%s]:[%s]->", p->key, p->value);
p = p->next;
}
printf("No Entry\n");
}
return 0;
}