Skip to content

Instantly share code, notes, and snippets.

@cs-fedy
Last active September 17, 2020 21:01
Show Gist options
  • Save cs-fedy/ae4db0ae97993e3b0d3018a3b18b67cc to your computer and use it in GitHub Desktop.
Save cs-fedy/ae4db0ae97993e3b0d3018a3b18b67cc to your computer and use it in GitHub Desktop.
hash table separate chaining implementation C.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "hashTable.h"
// TODO: rehash if load factor is passed.
// hash table functions
HashTable * createHashTable(int capacity) {
HashTable * ht = malloc(sizeof(HashTable));
int index;
ht -> size = 0;
ht -> capacity = capacity;
ht -> table = malloc(capacity * sizeof(LinkedList));
for (index = 0; index < ht -> capacity; index++) {
ht -> table[index] = createList();
}
return ht;
}
void insertItem(int key, HashTable * ht) {
int index = hashFunction(key, ht -> capacity);
append(ht ->table[index], key);
}
void deleteItem(int key, HashTable * ht) {
int index = hashFunction(key, ht -> capacity);
Node * nd = findNode(ht -> table[index] -> head, key);
if (nd == NULL) {
perror("key doesn't exist in the hash table");
exit(EXIT_FAILURE);
} else {
removeNode(nd);
}
}
Node * searchKey(int key, HashTable * ht) {
int index = hashFunction(key, ht -> capacity);
return findNode(ht -> table[index] -> head, key);
}
int hashFunction(int key, int capacity) {
return key % capacity;
}
void displayHashTable(HashTable * ht) {
int index;
for (index = 0; index < ht -> capacity; index++) {
if (ht -> table[index] -> head != NULL) {
printf("%d ==> ", index);
traverse(ht -> table[index] -> head, show);
}
}
}
// linked list functions
static LinkedList * createList() {
LinkedList *ll ;
ll = malloc(sizeof(LinkedList)) ;
ll -> head = NULL ;
ll -> tail = NULL ;
return ll ;
}
static unsigned isEmpty(LinkedList * ll) {
return (ll -> head == NULL) &&
(ll -> tail == NULL);
}
static void removeNode(Node * nd) {
Node * tmpNd;
assert(nd -> next);
tmpNd = nd -> next;
*nd = *tmpNd;
free(tmpNd);
}
static void append(LinkedList * ll, int key) {
Node * tmpNd = malloc(sizeof(Node));
tmpNd -> key = key;
tmpNd -> next = NULL;
if (isEmpty(ll))
ll -> head = ll -> tail = tmpNd;
else {
ll -> tail -> next = tmpNd;
ll -> tail = tmpNd;
}
}
static Node * findNode(Node * nd, int key) {
while (nd && nd -> key != key) {
nd = nd -> next;
}
return nd;
}
void show(Node * nd) {
printf("%d -- ", nd -> key);
}
void traverse(Node * nd, void(*callback)(Node * nd)) {
while (nd) {
(*callback)(nd);
nd = nd -> next;
}
printf("\n");
}
// Node of a linked list.
struct Node {
int key;
struct Node * next; // Pointer to next node in LL.
};
struct LinkedList {
struct Node * head;
struct Node * tail;
};
struct HashTable {
int size;
int capacity;
struct LinkedList ** table;
};
typedef struct Node Node;
typedef struct LinkedList LinkedList;
typedef struct HashTable HashTable;
// hash table functions
HashTable * createHashTable(int capacity);
void insertItem(int key, HashTable * ht);
void deleteItem(int key, HashTable * ht);
Node * searchKey(int key, HashTable * ht);
int hashFunction(int key, int capacity);
void displayHashTable(HashTable * ht);
// linked list functions
static LinkedList * createList(void);
static unsigned isEmpty(LinkedList * ll);
static void removeNode(Node * nd);
static void append(LinkedList * ll, int key);
static Node * findNode(Node * nd, int key);
void show(Node * nd);
void traverse(Node * nd, void(*callback)(Node * nd));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment