In open addressing every key lives directly in the table array. On collision the algorithm probes other slots in a fixed sequence. With linear probing it tries i, i+1, i+2, ... modulo the table size. Search follows the same sequence and stops at the key or an empty slot. The table must never fill completely, so keep the load factor low.
Remember
for (p = 0; p < SIZE; p++) {
s = (hash + p) % SIZE;
if (t[s] == EMPTY) { t[s] = key; return s; }
}
Reference
Cheat Sheet
Keep these facts handy while you work with Open Addressing in C.
#include <stdio.h>
#define SIZE 7
#define EMPTY (-1)
static int insert(int t[], int key) {
for (int p = 0; p < SIZE; p++) {
int s = (key % SIZE + p) % SIZE;
if (t[s] == EMPTY) { t[s] = key; return s; }
}
return -1;
}
static int search(const int t[], int key, int *probes) {
*probes = 0;
for (int p = 0; p < SIZE; p++) {
int s = (key % SIZE + p) % SIZE;
(*probes)++;
if (t[s] == key) return s;
if (t[s] == EMPTY) return -1;
}
return -1;
}
int main(void) {
int t[SIZE], probes;
for (int i = 0; i < SIZE; i++) t[i] = EMPTY;
int keys[] = {10, 17, 24, 3, 5};
for (int i = 0; i < 5; i++) insert(t, keys[i]);
printf("table:");
for (int i = 0; i < SIZE; i++) {
if (t[i] == EMPTY) printf(" -");
else printf(" %d", t[i]);
}
printf("\n");
if (search(t, 24, &probes) >= 0) printf("search 24: found in %d probes\n", probes);
if (search(t, 12, &probes) < 0) printf("search 12: not found after %d probes\n", probes);
return 0;
}
Output
table: 5 - - 10 17 24 3
search 24: found in 3 probes
search 12: not found after 4 probes
How It Works
10, 17, 24, and 3 all hash to slot 3, so they fill slots 3, 4, 5, 6.
Key 5 hashes to slot 5, finds 5 and 6 taken, and wraps to slot 0.
Searching 12 starts at slot 5 and probes 5, 6, 0, then stops at the empty slot 1.
Edge Cases & Pitfalls
Check these before relying on Open Addressing in real programs.
full
Full table
Probing a full table loops forever without the p < SIZE limit.
del
Deleting without tombstones
Emptying a slot breaks searches for keys placed after it.
Remember
Key Takeaways
Keys live in the array.
Probe sequence is deterministic.
Keep the table sparse.
One line: In open addressing every key lives directly in the table array.
Frequently Asked Questions
A collision strategy that stores all entries in the table array and probes alternative slots when the preferred slot is taken.
Probing slots sequentially (i, i+1, i+2, ...) with wraparound until a free slot is found.
Continue with "C Load Factor and Rehashing" next. Resize and rehash when the load factor passes a threshold. The C Hash Tables hub lists every topic in order.