Implement Hash Tables with Quadratic Probing in C++



<p>A hash table is a data structure which is used to store key-value pairs. Hash function is used by hash table to compute an index into an array in which an element will be inserted or searched. Quadratic probing is a collision resolving technique in Open Addressed Hash tables. It operates by taking the original hash index and adding successive values of an arbitrary quadratic polynomial until an open slot is found.</p><p>This is a C++ program to Implement Hash Tables with Quadratic Probing.</p><h2>Algorithm</h2><p><strong>For search a key value:</strong></p><pre class="result notranslate">Begin &nbsp; &nbsp;Declare function SearchKey(int k, HashTable *ht) &nbsp; &nbsp; &nbsp; int pos = HashFunc(k, ht-&gt;s) &nbsp; &nbsp; &nbsp; intialize collisions = 0 &nbsp; &nbsp; &nbsp; while (ht-&gt;t[pos].info != Emp and ht-&gt;t[pos].e != k) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos = pos + 2 * ++collisions -1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (pos &gt;= ht-&gt;s) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pos = pos - ht-&gt;s &nbsp; &nbsp; &nbsp; return pos End.</pre><p><strong>For Insert:</strong></p><pre class="result notranslate">Begin &nbsp; &nbsp;Declare function Insert(int k, HashTable *ht) &nbsp; &nbsp; &nbsp; int pos = SearchKey(k, ht) &nbsp; &nbsp; &nbsp; if (ht-&gt;t[pos].info != Legi) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ht-&gt;t[pos].info = Legi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ht-&gt;t[pos].e = k End.</pre><p><strong>For Display:</strong></p><pre class="result notranslate">Begin &nbsp; &nbsp;Declare function display(HashTable *ht) &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; ht-&gt;s; i++) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int value = ht-&gt;t[i].e &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!value) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print&quot;Position: &quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print the current position &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Print&quot; Element: Null&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print&quot;Position: &quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print the current position &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Print&quot; Element: &quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Print the element. End.</pre><p><strong>For Rehash Function:</strong></p><pre class="result notranslate">Begin &nbsp; &nbsp;Declare function Rehash(HashTable *ht) &nbsp; &nbsp; &nbsp; int s = ht-&gt;s &nbsp; &nbsp; &nbsp; HashTableEntry *t= ht-&gt;t &nbsp; &nbsp; &nbsp; ht= initiateTable(2 * s) &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; s; i++) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (t[i].info == Legi) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Insert(t[i].e, ht) &nbsp; &nbsp; &nbsp; free(t) &nbsp; &nbsp; &nbsp; return ht End. Source Code:</pre><h2>Example Code</h2><p><a class="demo" href="" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;iostream&gt; #include &lt;cstdlib&gt; #define T_S 10 using namespace std; enum EntryType { &nbsp; &nbsp;Legi, Emp, Del}; &nbsp; &nbsp;struct HashTableEntry { &nbsp; &nbsp; &nbsp; int e; &nbsp; &nbsp; &nbsp; enum EntryType info; &nbsp; &nbsp;}; &nbsp; &nbsp;struct HashTable { &nbsp; &nbsp; &nbsp; int s; &nbsp; &nbsp; &nbsp; HashTableEntry *t; &nbsp; &nbsp;}; &nbsp; &nbsp;bool isPrime (int n) { &nbsp; &nbsp;if (n == 2 || n == 3) &nbsp; &nbsp; &nbsp; return true; &nbsp; &nbsp;if (n == 1 || n % 2 == 0) &nbsp; &nbsp; &nbsp; return false; &nbsp; &nbsp;for (int i = 3; i * i &lt;= n; i += 2) &nbsp; &nbsp; &nbsp; if (n % i == 0) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false; &nbsp; &nbsp;return true; } int nextPrime(int n) { &nbsp; &nbsp;if (n &lt;= 0) &nbsp; &nbsp; &nbsp; n == 3; &nbsp; &nbsp;if (n % 2 == 0) &nbsp; &nbsp; &nbsp; n++; &nbsp; &nbsp;for (; !isPrime( n ); n += 2); &nbsp; &nbsp; &nbsp; return n; } int HashFunc(int k, int s) { &nbsp; &nbsp;return k % s; } HashTable *initiateTable(int s) { &nbsp; &nbsp;HashTable *ht; &nbsp; &nbsp;if (s &lt; T_S) { &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Table Size is Too Small&quot;&lt;&lt;endl; &nbsp; &nbsp; &nbsp; return NULL; &nbsp; &nbsp;} &nbsp; &nbsp;ht= new HashTable; &nbsp; &nbsp;if (ht == NULL) { &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Out of Space&quot;&lt;&lt;endl; &nbsp; &nbsp; &nbsp; return NULL; &nbsp; &nbsp;} &nbsp; &nbsp;ht-&gt;s = nextPrime(s); &nbsp; &nbsp;ht-&gt;t = new HashTableEntry [ht-&gt;s]; &nbsp; &nbsp;if (ht-&gt;t == NULL) { &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Table Size is Too Small&quot;&lt;&lt;endl; &nbsp; &nbsp; &nbsp; return NULL; &nbsp; &nbsp;} &nbsp; &nbsp;for (int i = 0; i &lt; ht-&gt;s; i++) { &nbsp; &nbsp; &nbsp; ht-&gt;t[i].info = Emp; &nbsp; &nbsp; &nbsp; ht-&gt;t[i].e = NULL; &nbsp; &nbsp;} &nbsp; &nbsp;return ht; } int SearchKey(int k, HashTable *ht) { &nbsp; &nbsp;int pos = HashFunc(k, ht-&gt;s); &nbsp; &nbsp;int collisions = 0; &nbsp; &nbsp;while (ht-&gt;t[pos].info != Emp &amp;&amp; ht-&gt;t[pos].e != k) { &nbsp; &nbsp; &nbsp; pos = pos + 2 * ++collisions -1; &nbsp; &nbsp; &nbsp; if (pos &gt;= ht-&gt;s) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos = pos - ht-&gt;s; &nbsp; &nbsp;} &nbsp; &nbsp;return pos; } void Insert(int k, HashTable *ht) { &nbsp; &nbsp;int pos = SearchKey(k, ht); &nbsp; &nbsp;if (ht-&gt;t[pos].info != Legi) { &nbsp; &nbsp; &nbsp; ht-&gt;t[pos].info = Legi; &nbsp; &nbsp; &nbsp; ht-&gt;t[pos].e = k; &nbsp; &nbsp;} } HashTable *Rehash(HashTable *ht) { &nbsp; &nbsp;int s = ht-&gt;s; &nbsp; &nbsp;HashTableEntry *t= ht-&gt;t; &nbsp; &nbsp;ht= initiateTable(2 * s); &nbsp; &nbsp;for (int i = 0; i &lt; s; i++) { &nbsp; &nbsp; &nbsp; if (t[i].info == Legi) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Insert(t[i].e, ht); &nbsp; &nbsp;} &nbsp; &nbsp;free(t); &nbsp; &nbsp;return ht; } void display(HashTable *ht) { &nbsp; &nbsp;for (int i = 0; i &lt; ht-&gt;s; i++) { &nbsp; &nbsp; &nbsp; int value = ht-&gt;t[i].e; &nbsp; &nbsp; &nbsp; if (!value) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout&lt;&lt;&quot;Position: &quot;&lt;&lt;i + 1&lt;&lt;&quot; Element: Null&quot;&lt;&lt;endl; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout&lt;&lt;&quot;Position: &quot;&lt;&lt;i + 1&lt;&lt;&quot; Element: &quot;&lt;&lt;value&lt;&lt;endl; &nbsp; &nbsp;} } int main() { &nbsp; &nbsp;int v, s, pos, i = 1; &nbsp; &nbsp;int c; &nbsp; &nbsp;HashTable *ht; &nbsp; &nbsp;while(1) { &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;1.Initialize size of the table&quot;&lt;&lt;endl; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;2.Insert element into the table&quot;&lt;&lt;endl; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;3.Display Hash Table&quot;&lt;&lt;endl; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;4.Rehash The Table&quot;&lt;&lt;endl; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;5.Exit&quot;&lt;&lt;endl; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Enter your choice: &quot;; &nbsp; &nbsp; &nbsp; cin&gt;&gt;c; &nbsp; &nbsp; &nbsp; switch(c) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 1: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Enter size of the Hash Table: &quot;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cin&gt;&gt;s; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ht = initiateTable(s); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Size of Hash Table: &quot;&lt;&lt;nextPrime(s); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 2: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i &gt; ht-&gt;s) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout&lt;&lt;&quot;Table is Full, Rehash the table&quot;&lt;&lt;endl; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;continue; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;Enter element to be inserted: &quot;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cin&gt;&gt;v; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Insert(v, ht); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 3: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display(ht); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 4: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ht = Rehash(ht); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 5: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit(1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cout&lt;&lt;&quot;\nEnter correct option\n&quot;; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><pre class="result notranslate">1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 1 Enter size of the Hash Table: 4 Table Size is Too Small Size of Hash Table: 51.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 1 Enter size of the Hash Table: 10 Size of Hash Table: 111.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 1 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 2 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 3 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 4 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 5 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 6 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 7 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 8 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 9 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 10 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 11 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Table is Full, Rehash the table 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 3 Position: 1 Element: 11 Position: 2 Element: 1 Position: 3 Element: 2 Position: 4 Element: 3 Position: 5 Element: 4 Position: 6 Element: 5 Position: 7 Element: 6 Position: 8 Element: 7 Position: 9 Element: 8 Position: 10 Element: 9 Position: 11 Element: 10 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 4 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 3 Position: 1 Element: Null Position: 2 Element: 1 Position: 3 Element: 2 Position: 4 Element: 3 Position: 5 Element: 4 Position: 6 Element: 5 Position: 7 Element: 6 Position: 8 Element: 7 Position: 9 Element: 8 Position: 10 Element: 9 Position: 11 Element: 10 Position: 12 Element: 11 Position: 13 Element: Null Position: 14 Element: Null Position: 15 Element: Null Position: 16 Element: Null Position: 17 Element: Null Position: 18 Element: Null Position: 19 Element: Null Position: 20 Element: Null Position: 21 Element: Null Position: 22 Element: Null Position: 23 Element: Null 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 2 Enter element to be inserted: 20 1.Initialize size of the table 2.Insert element into the table 3.Display Hash Table 4.Rehash The Table 5.Exit Enter your choice: 5</pre>

Updated on: 2025-04-24T17:28:56+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements