23 std::shared_ptr<struct Node> next;
26 std::vector<std::shared_ptr<Node>>
head;
36 while (mod--)
head.push_back(
nullptr);
46 std::shared_ptr<Node> curr;
47 std::shared_ptr<Node> temp(
new Node);
55 while (curr->next) curr = curr->next;
64 std::shared_ptr<Node> temp =
nullptr;
66 for (i = 0; i <
_mod; i++) {
68 std::cout <<
"Key " << i <<
" is empty" << std::endl;
70 std::cout <<
"Key " << i <<
" has values = " << std::endl;
73 std::cout << temp->data <<
" " << std::endl;
76 std::cout << temp->data;
77 std::cout << std::endl;
91 virtual int hash(
int x)
const {
return x %
_mod; }
102 std::shared_ptr<Node> temp =
head[
h];
105 std::cout <<
"Element not found" << std::endl;
110 while (temp->data != x && temp->next) temp = temp->next;
113 std::cout <<
"Element found" << std::endl;
119 if (temp->data == x) {
120 std::cout <<
"Element found" << std::endl;
125 std::cout <<
"Element not found" << std::endl;
134 int c = 0, x = 0, mod = 0,
h = 0;
135 std::cout <<
"Enter the size of Hash Table. = " << std::endl;
142 std::cout << std::endl;
143 std::cout <<
"PLEASE CHOOSE -" << std::endl;
144 std::cout <<
"1. Add element." << std::endl;
145 std::cout <<
"2. Find element." << std::endl;
146 std::cout <<
"3. Generate Hash." << std::endl;
147 std::cout <<
"4. Display Hash table." << std::endl;
148 std::cout <<
"5. Exit." << std::endl;
152 std::cout <<
"Enter element to add = " << std::endl;
159 std::cout <<
"Enter element to search = " << std::endl;
165 std::cout <<
"Enter element to generate hash = " << std::endl;
167 std::cout <<
"Hash of " << x <<
" is = " << mychain.
hash(x)
177 std::cout << std::endl;
Chain class with a given modulus.
bool find(int x, int h) const
Find if a value and corresponding hash exist.
void add(int x, int h)
create and add a new node with a give value and at a given height
void display()
Display the chain.
hash_chain(int mod)
Construct a new chain object.
virtual int hash(int x) const
Compute the hash of a value for current chain.
std::vector< std::shared_ptr< Node > > head
array of nodes
int _mod
modulus of the class