Skip to content

Commit 30c8dcc

Browse files
committed
Added C++ Hash table implementation
Uses separate chaining to resolve collisions
1 parent c63c85f commit 30c8dcc

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#ifndef _HASH_TABLE
2+
#define _HASH_TABLE
3+
4+
// A hash table implemented using Horner's rule and separate chaining
5+
6+
#include <vector>
7+
#include <list>
8+
#include <string>
9+
#include <iostream>
10+
#include <algorithm>
11+
12+
class Hashtable
13+
{
14+
private:
15+
unsigned int size;
16+
std::vector<std::list<std::string>> table;
17+
18+
// calculate hash using horner's rule for values of type std::string
19+
unsigned int calculateHash(const std::string& key) const {
20+
unsigned hash = 0;
21+
22+
for (char ch: key) {
23+
hash = 37 * hash + static_cast<unsigned>(ch);
24+
}
25+
26+
return hash % size;
27+
}
28+
29+
// check the linked list for a value
30+
// return true if found and false other wise
31+
inline bool checkList(std::list<std::string>& linkedList, const std::string& value) const {
32+
if (std::find(begin(linkedList), end(linkedList), value) != end(linkedList)) {
33+
return true;
34+
}
35+
return false;
36+
}
37+
38+
// Prints an std::list
39+
inline void print(const std::list<std::string>& list, std::ostream& os = std::cout) const {
40+
for (auto& val : list) { os << "[" << val << "]->"; }
41+
}
42+
43+
public:
44+
// Constructor - tableSize is the size of the table
45+
// the defualt size is 11
46+
explicit Hashtable(int tableSize = 11)
47+
:size(tableSize)
48+
{
49+
table = std::vector<std::list<std::string>>(tableSize);
50+
}
51+
52+
// adds an element to the table
53+
void insert(const std::string& value) {
54+
auto& chainList = table[calculateHash(value)];
55+
56+
// check if value exists in linked list
57+
if (!checkList(chainList, value)) {
58+
chainList.push_back(value);
59+
}
60+
}
61+
62+
// Removes an element from the table
63+
void remove(const std::string& value) {
64+
auto& chainList = table[calculateHash(value)];
65+
66+
if (std::find(begin(chainList), end(chainList), value) != end(chainList)) {
67+
// element found remove it
68+
chainList.remove(value);
69+
std::cout << "value removed\n";
70+
}
71+
}
72+
73+
// Prints the hash table
74+
void print(std::ostream& os = std::cout) const {
75+
for (auto& val : table) {
76+
print(val, os);
77+
}
78+
os << "\n";
79+
}
80+
};
81+
82+
#endif // _HASH_TABLE
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
#include "hashtable.h"
3+
4+
int main() {
5+
Hashtable table;
6+
table.insert("abc");
7+
table.insert("yoga");
8+
table.insert("abc"); // adding value again not allowed
9+
10+
table.remove("xyz"); // value not removed -- does not exist in chain
11+
table.remove("abc"); // value removed
12+
}

0 commit comments

Comments
 (0)