Colision Resolution ( By separate chaining using linked list )
We have seen hash implementation in Array , where we can fill only one value in one slot. If new value comes it overwrites previous value. But using collision resolution by linked list we can resolve this problem and preserve the values. Whenever new value comes to the slot which is already filled then we can put new value in linked list associated with that slot.
In this method performance degrades when more values are matching with same slot.
The below picture is for hash table with collision resolution using channed linked list.
Hash function is key % 5
C Code for Collision Resolution in Hash Table is below
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 |
/* Hash Table Collision Resolution code by codingstreet.com */ #include<stdio.h> #include<stdlib.h> struct hash { struct hashnode *ptr; }; struct hashnode { int val; struct hashnode *next; }; struct hash *create_hash(int size) { struct hash *htable; int i; htable = (struct hash *) malloc(sizeof(struct hash) * size); if (htable == NULL) { printf("\n Out of Memory !!"); return NULL; } for (i = 0; i < size; i++) htable[i].ptr = NULL; return htable; } struct hashnode *create_hashnode(int val) { struct hashnode *tmp; tmp = (struct hashnode *) malloc(sizeof(struct hashnode)); if (tmp == NULL) printf("\n System out of Memory"); tmp->next = NULL; tmp->val = val; } int insert_value(struct hash **htable, int val, int hsize) { int slot; struct hash *htptr = (*htable); struct hashnode *tmp; if ((*htable) == NULL) { printf("\n Hash Table is not created"); return 0; } slot = (val) % hsize; if (htptr[slot].ptr == NULL) { tmp = create_hashnode(val); htptr[slot].ptr = tmp; } else { tmp = create_hashnode(val); tmp->next = htptr[slot].ptr; htptr[slot].ptr = tmp; } return 1; } void print_hashtable(struct hash **htable, int size) { int i; struct hashnode *tmp; struct hash *htmp = (*htable); for (i = 0; i < size; i++) { tmp = htmp[i].ptr; while (tmp != NULL) { printf(" %d ", tmp->val); tmp = tmp->next; } printf("\n"); } } int seach_hashtable(struct hash **htable, int val, int size) { int slot, found = 0; slot = val % size; struct hashnode *tmp; tmp = (*htable)[slot].ptr; while (tmp != NULL) { if (tmp->val == val) { found = 1; break; } tmp = tmp->next; } return found; } int main() { struct hash *hashtable1; int hashtable1size; printf("\n Enter the size of HashTable : "); scanf("%d", &hashtable1size); hashtable1 = create_hash(hashtable1size); if (hashtable1 != NULL) printf("\nTable Created Successfully !!"); int key, val, found; do { printf("\n Hash Table Collision Resolution by Codingstreet.com"); printf("\n 1. Insert value "); printf("\n 2. Search Value"); printf("\n 3. Print Table"); printf("\n 4. Exit"); printf("\n Enter the key [1-4] :"); scanf("%d", &key); switch (key) { case 1: printf("\n Enter Val : "); scanf("%d", &val); insert_value(&hashtable1, val, hashtable1size); break; case 2: printf("\n Enter Val : "); scanf("%d", &val); found = seach_hashtable(&hashtable1, val, hashtable1size); if (found == 0) printf("\n Val not found"); else printf("\n %d found at slot %d ", val, val % hashtable1size); break; case 3: print_hashtable(&hashtable1, hashtable1size); break; case 4: printf("\n Thank you !!! "); break; default: break; } } while (key != 4); printf("\n Thanks for using Codingstreet.com 's Data structure solution\n"); return 0; } |