In open addressing every key lives inside the table itself. When a key's home slot is taken, linear probing simply tries the next slot, then the next, wrapping around at the end. The rules are short, but deleting needs care, and the table slows down sharply as it fills.
A key k starts at its home slot h(k) = k % m, where m is the table size. If that slot is not the one we want, try h(k)+1, h(k)+2, …, all mod m. Every operation walks this same sequence.
Insert: probe until an empty slot (or a deleted one) and put the key there. Search: probe until you find the key, or reach an empty slot, which proves the key is absent. Delete: find the key, then mark the slot deleted instead of emptying it.
Search stops at the first empty slot. If delete just emptied a slot, a key placed after it in the same run would become unreachable. A deleted marker (a "tombstone") says "keep probing." Insert may reuse a tombstone; search must walk past it.
Keys that collide pile up in runs of filled slots called clusters, and a key landing anywhere in a cluster makes it longer (primary clustering). The load factor α = n / m tells you how full the table is. Keep α below about 0.7.
HashNode* arr[10]With a good hash function and a table that is not too full, each operation looks at only a couple of slots, so it is O(1) on average. When the table is nearly full, or many keys share one home slot, a probe can walk across most of the table: O(N).
| Operation | Best case | Average case | Worst case | Extra space |
|---|---|---|---|---|
| Insert | O(1) | O(1) good hash, O(N) bad hash | O(N) | O(1) |
| Search | O(1) | O(1) good hash, O(N) bad hash | O(N) | O(1) |
| Delete | O(1) | O(1) good hash, O(N) bad hash | O(N) | O(1) |
How full is too full? For linear probing the expected number of probes grows like 1 / (1 − α)². At α = 0.5 an unsuccessful search takes about 2.5 probes; at α = 0.9 it takes about 50. The Numbers panel above shows these estimates for the current table. When α gets high, real hash tables allocate a bigger array and re-insert every key (rehashing). Changing the capacity in the lab does exactly that.
The HashMap in the code panel handles four cases that are easy to get wrong:
insertNode remembers the first tombstone it passes, but keeps probing until an empty slot to make sure the key is not already stored further along. Only then does it reuse the tombstone.arr[i]->value in place instead of allocating a new node, so no memory is leaked.-7 % 10 is -7, which is not a valid index. hashCode uses ((key % capacity) + capacity) % capacity so the index is always 0 to capacity − 1.dummy node. The code always tests arr[i] == dummy before comparing keys, so no real key can be mistaken for it.