C Example: Hash Map: separate chaining and load-factor observation

🗓 Last updated: 2026-11-30👤 Written by: Ivy✓ Reviewed by: Alex🏷 Category: Data Structures

A string-to-int hash map with seven buckets and separate chaining: the hash accumulates each character in base 31 and takes the modulus; insert, update, lookup, delete and traversal are supported. Prints each bucket chain, the element count and load factor, and shows duplicate-key updates and deletion.

🔥 Advanced Reference entries: kw-structfn-mallocfn-free
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
C · GCC 10.2 · -std=c11
put apple       -> inserted
put banana      -> inserted
put cherry      -> inserted
put date        -> inserted
put elderberry  -> inserted
put fig         -> inserted
put grape       -> inserted
put apple       -> updated
bucket 0:
bucket 1: apple=99
bucket 2:
bucket 3: elderberry=13 cherry=7 banana=5
bucket 4: date=11
bucket 5: grape=19
bucket 6: fig=17
size=7 load=1.00

get apple    -> found (99)
get missing  -> missing
remove date  -> removed
bucket 0:
bucket 1: apple=99
bucket 2:
bucket 3: elderberry=13 cherry=7 banana=5
bucket 4:
bucket 5: grape=19
bucket 6: fig=17
size=6 load=0.86
🧩 Keep Learning

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏