Home C Hash Tables Hash Table Insertion and Searching in C Hash Table Insertion and Searching in C Definition
Insert, Update, and Find Insertion hashes the key, scans the chain for an existing entry (update) and otherwise adds a new one. Search hashes again and walks the same chain. String keys need strcmp for equality and must be copied into the table if the original may change. Both operations are O(1) on average and O(chain length) worst case.
unsigned i = hash(word) % B;
for (n = t[i]; n; n = n->next)
if (strcmp(n->word, word) == 0) { n->count++; return; }
/* else allocate, copy word, link in front */ Reference
Cheat Sheet Keep these facts handy while you work with Hash Table Insertion and Searching in C in C.
Topic Detail Equality strcmp(a, b) == 0Copy keys malloc + strcpyAverage O(1) Worst O(n) if all collide
Code
C Examples Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Hash Table Deletion in C .
1. Word frequency counter Example 1 — Word frequency counter Count the words in "to be or not to be".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define B 8
struct Node { char *word; int count; struct Node *next; };
static unsigned hash(const char *s) {
unsigned h = 5381;
while (*s) h = h * 33 + (unsigned char)*s++;
return h % B;
}
static void add(struct Node *t[], const char *w) {
unsigned i = hash(w);
for (struct Node *n = t[i]; n; n = n->next)
if (strcmp(n->word, w) == 0) { n->count++; return; }
struct Node *n = malloc(sizeof *n);
if (!n) exit(1);
n->word = malloc(strlen(w) + 1);
if (!n->word) exit(1);
strcpy(n->word, w);
n->count = 1;
n->next = t[i];
t[i] = n;
}
static int count(struct Node *t[], const char *w) {
for (struct Node *n = t[hash(w)]; n; n = n->next)
if (strcmp(n->word, w) == 0) return n->count;
return 0;
}
int main(void) {
struct Node *table[B] = {0};
const char *text[] = {"to", "be", "or", "not", "to", "be"};
const char *ask[] = {"to", "be", "or", "not", "maybe"};
for (int i = 0; i < 6; i++) add(table, text[i]);
for (int i = 0; i < 5; i++) printf("%s: %d\n", ask[i], count(table, ask[i]));
for (int i = 0; i < B; i++) {
struct Node *n = table[i];
while (n) { struct Node *nx = n->next; free(n->word); free(n); n = nx; }
}
return 0;
} to: 2
be: 2
or: 1
not: 1
maybe: 0 How It Works A repeated word finds its existing node and increments the count. A new word is copied with malloc + strcpy and linked into its bucket. Searching a missing word returns 0. Edge Cases & Pitfalls Check these before relying on Hash Table Insertion and Searching in C in real programs.
alias Storing the caller's pointer If you store w itself and the caller reuses the buffer, your key changes. Copy it.
cmp Comparing pointers Use strcmp, not ==, to compare strings.
Remember
Key Takeaways One line: Insertion hashes the key, scans the chain for an existing entry (update) and otherwise adds a new one.
Frequently Asked Questions How do you search a hash table? Compute the hash, go to that slot, and compare keys along the chain (or probe sequence) until you find the key or an empty slot.
Why copy string keys? The caller may modify or free its buffer, which would corrupt keys stored by pointer.
What should I learn after Hash Table Insertion and Searching in C? Continue with "Hash Table Deletion in C" next. Unlink an entry from its chain and free it. The C Hash Tables hub lists every topic in order.
Next: Hash Table Deletion in C Unlink an entry from its chain and free it.
Continue → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion