{"id":741,"date":"2024-10-19T13:52:01","date_gmt":"2024-10-19T08:22:01","guid":{"rendered":"https:\/\/codexplained.in\/?p=741"},"modified":"2025-11-24T15:34:08","modified_gmt":"2025-11-24T10:04:08","slug":"implement-hash-table-using-chaining","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=741","title":{"rendered":"Implement Hash Table using Chaining"},"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 the structure for the nodes in the linked list\nstruct Node {\n    int data;\n    struct Node* next;\n};\n\n\/\/ Function to create a new node with given data\nstruct Node* createNode(int data) {\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\n    newNode-&gt;data = data;\n    newNode-&gt;next = NULL;\n    return newNode;\n}\n\n\/\/ Function to insert a value into the hash table\nvoid insert(struct Node* hashTable&#x5B;], int key, int tableSize) {\n    int index = key % tableSize;\n    struct Node* newNode = createNode(key);\n    newNode-&gt;next = hashTable&#x5B;index];\n    hashTable&#x5B;index] = newNode;\n}\n\n\/\/ Function to search for a value in the hash table\nint search(struct Node* hashTable&#x5B;], int key, int tableSize) {\n    int index = key % tableSize;\n    struct Node* temp = hashTable&#x5B;index];\n    while (temp) {\n        if (temp-&gt;data == key) {\n            return 1; \/\/ Key found\n        }\n        temp = temp-&gt;next;\n    }\n    return 0; \/\/ Key not found\n}\n\n\/\/ Function to delete a value from the hash table\nvoid delete(struct Node* hashTable&#x5B;], int key, int tableSize) {\n    int index = key % tableSize;\n    struct Node* temp = hashTable&#x5B;index];\n    struct Node* prev = NULL;\n\n    \/\/ Traverse the linked list at that index\n    while (temp != NULL &amp;&amp; temp-&gt;data != key) {\n        prev = temp;\n        temp = temp-&gt;next;\n    }\n\n    \/\/ If the key is not present\n    if (temp == NULL) {\n        printf(&quot;Key %d not found.\\n&quot;, key);\n        return;\n    }\n\n    \/\/ Key is present at the head of the linked list\n    if (prev == NULL) {\n        hashTable&#x5B;index] = temp-&gt;next;\n    } else {\n        prev-&gt;next = temp-&gt;next;\n    }\n\n    free(temp);\n    printf(&quot;Key %d deleted.\\n&quot;, key);\n}\n\n\/\/ Function to display the hash table\nvoid display(struct Node* hashTable&#x5B;], int tableSize) {\n    for (int i = 0; i &lt; tableSize; i++) {\n        printf(&quot;HashTable&#x5B;%d]: &quot;, i);\n        struct Node* temp = hashTable&#x5B;i];\n        while (temp) {\n            printf(&quot;%d -&gt; &quot;, temp-&gt;data);\n            temp = temp-&gt;next;\n        }\n        printf(&quot;NULL\\n&quot;);\n    }\n}\n\n\/\/ Main function\nint main() {\n    int tableSize = 10; \/\/ Define size of the hash table\n    struct Node* hashTable&#x5B;tableSize];\n\n    \/\/ Initialize the hash table\n    for (int i = 0; i &lt; tableSize; i++) {\n        hashTable&#x5B;i] = NULL;\n    }\n\n    \/\/ Insert elements into the hash table\n    insert(hashTable, 5, tableSize);\n    insert(hashTable, 15, tableSize);\n    insert(hashTable, 25, tableSize);\n    insert(hashTable, 35, tableSize);\n\n    printf(&quot;Hash table after insertion:\\n&quot;);\n    display(hashTable, tableSize);\n\n    \/\/ Search for a key in the hash table\n    int searchKey = 15;\n    if (search(hashTable, searchKey, tableSize)) {\n        printf(&quot;Key %d found.\\n&quot;, searchKey);\n    } else {\n        printf(&quot;Key %d not found.\\n&quot;, searchKey);\n    }\n\n    \/\/ Delete a key from the hash table\n    delete(hashTable, 15, tableSize);\n    printf(&quot;Hash table after deletion:\\n&quot;);\n    display(hashTable, tableSize);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Structure of Node<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>struct Node<\/code> represents each node in a linked list.<\/li>\n\n\n\n<li>Each node stores an integer <code>data<\/code> and a pointer <code>next<\/code> to the next node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Creating a Node<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>createNode<\/code> function allocates memory for a new node, assigns the data, and initializes the <code>next<\/code> pointer to <code>NULL<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Hash Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The hash function used here is a simple modulus operation (<code>key % tableSize<\/code>), which calculates the index where a particular key should be stored.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Insert Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>insert<\/code> function calculates the index using the hash function.<\/li>\n\n\n\n<li>It creates a new node with the given key and inserts it at the beginning of the linked list at that index.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Search Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>search<\/code> function calculates the index and traverses the linked list at that index.<\/li>\n\n\n\n<li>If a node with the key is found, it returns <code>1<\/code> (key found).<\/li>\n\n\n\n<li>Otherwise, it returns <code>0<\/code> (key not found).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Delete Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>delete<\/code> function calculates the index and searches for the node with the given key.<\/li>\n\n\n\n<li>If the node is found, it adjusts the pointers to remove the node from the linked list and frees its memory.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Display Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>display<\/code> function iterates through the hash table.<\/li>\n\n\n\n<li>For each index, it prints the keys stored in the linked list at that index.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>It initializes the hash table with a given size.<\/li>\n\n\n\n<li>It inserts values into the hash table, displays it, searches for a value, deletes a value, and displays the updated hash table.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Hash table after insertion:\nHashTable&#091;0]: NULL\nHashTable&#091;1]: NULL\nHashTable&#091;2]: NULL\nHashTable&#091;3]: NULL\nHashTable&#091;4]: NULL\nHashTable&#091;5]: 35 -&gt; 25 -&gt; 15 -&gt; 5 -&gt; NULL\nHashTable&#091;6]: NULL\nHashTable&#091;7]: NULL\nHashTable&#091;8]: NULL\nHashTable&#091;9]: NULL\n\nKey 15 found.\nKey 15 deleted.\n\nHash table after deletion:\nHashTable&#091;0]: NULL\nHashTable&#091;1]: NULL\nHashTable&#091;2]: NULL\nHashTable&#091;3]: NULL\nHashTable&#091;4]: NULL\nHashTable&#091;5]: 35 -&gt; 25 -&gt; 5 -&gt; NULL\nHashTable&#091;6]: NULL\nHashTable&#091;7]: NULL\nHashTable&#091;8]: NULL\nHashTable&#091;9]: NULL\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p>This implementation of a hash table using chaining effectively handles collisions by storing keys in a linked list at each index. It allows for efficient insertion, searching, and deletion of keys. By chaining, we can manage cases where multiple keys hash to the same index without loss of data.<\/p>\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 Output Summary This implementation of a hash table using chaining effectively handles collisions by storing keys in a linked list at each index. It allows for efficient insertion, searching, and deletion of keys. By chaining, we can manage cases where multiple keys hash to the same index without loss of data.<\/p>\n","protected":false},"author":40,"featured_media":862,"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-741","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\/741","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=741"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/741\/revisions"}],"predecessor-version":[{"id":1393,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/741\/revisions\/1393"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/862"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}