C Hash Table Implementation

Intermediate
6 min read
Updated: Nov 2026
1 example

A Complete Table API

A practical table bundles a bucket array, an entry struct (key, value, next), and a small API: put (insert or update), get (lookup), remove, and destroy. The example uses separate chaining with 7 buckets and integer keys so the structure is easy to follow.

Remember
struct Entry { int key, value; struct Entry *next; };
struct Table { struct Entry *bucket[BUCKETS]; };
void put(struct Table *t, int key, int value);
int  get(const struct Table *t, int key, int *value);

Cheat Sheet

Keep these facts handy while you work with Hash Table Implementation in C.

TopicDetail
BucketHead of a chain
putInsert or update
getFind by key
destroyFree every entry

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Hash Table Insertion and Searching in C.

Example 1 — put, get, and destroy

Keys 1 and 8 share bucket 1 because 8 % 7 is 1.

C
#include <stdio.h>
#include <stdlib.h>
#define BUCKETS 7

struct Entry { int key, value; struct Entry *next; };
struct Table { struct Entry *bucket[BUCKETS]; };

static unsigned slot(int key) { return (unsigned)key % BUCKETS; }

static void put(struct Table *t, int key, int value) {
    unsigned i = slot(key);
    for (struct Entry *e = t->bucket[i]; e; e = e->next)
        if (e->key == key) { e->value = value; return; }
    struct Entry *n = malloc(sizeof *n);
    if (!n) exit(1);
    n->key = key;
    n->value = value;
    n->next = t->bucket[i];
    t->bucket[i] = n;
}

static int get(const struct Table *t, int key, int *value) {
    for (const struct Entry *e = t->bucket[slot(key)]; e; e = e->next)
        if (e->key == key) { *value = e->value; return 1; }
    return 0;
}

static void destroy(struct Table *t) {
    for (int i = 0; i < BUCKETS; i++) {
        struct Entry *e = t->bucket[i];
        while (e) { struct Entry *n = e->next; free(e); e = n; }
        t->bucket[i] = NULL;
    }
}

int main(void) {
    struct Table t = {{0}};
    int v;

    put(&t, 1, 100);
    put(&t, 2, 200);
    put(&t, 8, 800);

    if (get(&t, 1, &v)) printf("get(1)=%d\n", v);
    if (get(&t, 8, &v)) printf("get(8)=%d\n", v);
    if (!get(&t, 5, &v)) printf("get(5) missing\n");

    destroy(&t);
    return 0;
}

How It Works

  1. put first searches the chain so duplicate keys update instead of inserting twice.
  2. Keys 1 and 8 live in the same chain but are still found separately.
  3. destroy frees every entry in every bucket.

Edge Cases & Pitfalls

Check these before relying on Hash Table Implementation in real programs.

dup

Duplicate keys

Without the search in put, the same key can be stored twice.

init

Uninitialized buckets

Zero the table ({{0}}) so empty buckets are NULL.

Key Takeaways

  • Buckets plus entries.
  • put updates or inserts.
  • Free every chain.

One line: A practical table bundles a bucket array, an entry struct (key, value, next), and a small API: put (insert or update), get (lookup), remove, and destroy.

Frequently Asked Questions

Create an array of buckets, a hash function, and put/get/remove functions; handle collisions with chaining or probing.
In chaining, each bucket points to a linked list of entry structs.
Continue with "Hash Table Insertion and Searching in C" next. Count words with a string-keyed table. The C Hash Tables hub lists every topic in order.

Next: Hash Table Insertion and Searching in C

Count words with a string-keyed table.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful