TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
chaining.cpp
Go to the documentation of this file.
1
8#include <cmath>
9#include <iostream>
10#include <memory>
11#include <vector>
12
17 private:
21 using Node = struct Node {
22 int data{};
23 std::shared_ptr<struct Node> next;
24 };
25
26 std::vector<std::shared_ptr<Node>> head;
27 int _mod;
28
29 public:
35 explicit hash_chain(int mod) : _mod(mod) {
36 while (mod--) head.push_back(nullptr);
37 }
38
45 void add(int x, int h) {
46 std::shared_ptr<Node> curr;
47 std::shared_ptr<Node> temp(new Node);
48 temp->data = x;
49 temp->next = nullptr;
50 if (!head[h]) {
51 head[h] = temp;
52 curr = head[h];
53 } else {
54 curr = head[h];
55 while (curr->next) curr = curr->next;
56 curr->next = temp;
57 }
58 }
59
63 void display() {
64 std::shared_ptr<Node> temp = nullptr;
65 int i = 0;
66 for (i = 0; i < _mod; i++) {
67 if (!head[i]) {
68 std::cout << "Key " << i << " is empty" << std::endl;
69 } else {
70 std::cout << "Key " << i << " has values = " << std::endl;
71 temp = head[i];
72 while (temp->next) {
73 std::cout << temp->data << " " << std::endl;
74 temp = temp->next;
75 }
76 std::cout << temp->data;
77 std::cout << std::endl;
78 }
79 }
80 }
81
91 virtual int hash(int x) const { return x % _mod; }
92
101 bool find(int x, int h) const {
102 std::shared_ptr<Node> temp = head[h];
103 if (!head[h]) {
104 // index does not exist!
105 std::cout << "Element not found" << std::endl;
106 return false;
107 }
108
109 // scan for data value
110 while (temp->data != x && temp->next) temp = temp->next;
111
112 if (temp->next) {
113 std::cout << "Element found" << std::endl;
114 return true;
115 }
116
117 // implicit else condition
118 // i.e., temp->next == nullptr
119 if (temp->data == x) {
120 std::cout << "Element found" << std::endl;
121 return true;
122 }
123
124 // further implicit else condition
125 std::cout << "Element not found" << std::endl;
126 return false;
127 }
128};
129
133int main() {
134 int c = 0, x = 0, mod = 0, h = 0;
135 std::cout << "Enter the size of Hash Table. = " << std::endl;
136 std::cin >> mod;
137
138 hash_chain mychain(mod);
139
140 bool loop = true;
141 while (loop) {
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;
149 std::cin >> c;
150 switch (c) {
151 case 1:
152 std::cout << "Enter element to add = " << std::endl;
153 std::cin >> x;
154 h = mychain.hash(x);
155 h = std::abs(h);
156 mychain.add(x, h);
157 break;
158 case 2:
159 std::cout << "Enter element to search = " << std::endl;
160 std::cin >> x;
161 h = mychain.hash(x);
162 mychain.find(x, h);
163 break;
164 case 3:
165 std::cout << "Enter element to generate hash = " << std::endl;
166 std::cin >> x;
167 std::cout << "Hash of " << x << " is = " << mychain.hash(x)
168 << std::endl;
169 break;
170 case 4:
171 mychain.display();
172 break;
173 default:
174 loop = false;
175 break;
176 }
177 std::cout << std::endl;
178 }
179 /*add(1,&head1);
180 add(2,&head1);
181 add(3,&head2);
182 add(5,&head1);
183 display(&head1);
184 display(&head2);*/
185 return 0;
186}
int main()
Definition chaining.cpp:133
Chain class with a given modulus.
Definition chaining.cpp:16
bool find(int x, int h) const
Find if a value and corresponding hash exist.
Definition chaining.cpp:101
void add(int x, int h)
create and add a new node with a give value and at a given height
Definition chaining.cpp:45
void display()
Display the chain.
Definition chaining.cpp:63
hash_chain(int mod)
Construct a new chain object.
Definition chaining.cpp:35
virtual int hash(int x) const
Compute the hash of a value for current chain.
Definition chaining.cpp:91
std::vector< std::shared_ptr< Node > > head
array of nodes
Definition chaining.cpp:26
int _mod
modulus of the class
Definition chaining.cpp:27
int h(int key)
int data[MAX]
test data