-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaining.cpp
More file actions
141 lines (127 loc) · 3.85 KB
/
Copy pathchaining.cpp
File metadata and controls
141 lines (127 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* @file chaining.cpp
* @author [vasutomar](https://github.com/vasutomar)
* @author [Krishna Vedala](https://github.com/kvedala)
* @brief Implementation of [hash
* chains](https://en.wikipedia.org/wiki/Hash_chain).
*/
#include <cmath>
#include <iostream>
#include <memory>
#include <vector>
class hash_chain {
private:
using Node = struct Node {
int data{};
std::shared_ptr<struct Node> next;
};
std::vector<std::shared_ptr<Node>> head;
int _mod;
public:
explicit hash_chain(int mod) : _mod(mod) {
while (mod--) head.push_back(nullptr);
}
void add(int x, int h) {
std::shared_ptr<Node> curr;
std::shared_ptr<Node> temp(new Node);
temp->data = x;
temp->next = nullptr;
if (!head[h]) {
head[h] = temp;
curr = head[h];
} else {
curr = head[h];
while (curr->next) curr = curr->next;
curr->next = temp;
}
}
void display() {
std::shared_ptr<Node> temp = nullptr;
int i = 0;
for (i = 0; i < _mod; i++) {
if (!head[i]) {
std::cout << "Key " << i << " is empty" << std::endl;
} else {
std::cout << "Key " << i << " has values = " << std::endl;
temp = head[i];
while (temp->next) {
std::cout << temp->data << " " << std::endl;
temp = temp->next;
}
std::cout << temp->data;
std::cout << std::endl;
}
}
}
virtual int hash(int x) const { return x % _mod; }
bool find(int x, int h) const {
std::shared_ptr<Node> temp = head[h];
if (!head[h]) {
std::cout << "Element not found" << std::endl;
return false;
}
while (temp->data != x && temp->next) temp = temp->next;
if (temp->next) {
std::cout << "Element found" << std::endl;
return true;
}
if (temp->data == x) {
std::cout << "Element found" << std::endl;
return true;
}
std::cout << "Element not found" << std::endl;
return false;
}
};
int main() {
int c = 0, x = 0, mod = 0, h = 0;
std::cout << "Enter the size of Hash Table. = " << std::endl;
std::cin >> mod;
hash_chain mychain(mod);
bool loop = true;
while (loop) {
std::cout << std::endl;
std::cout << "PLEASE CHOOSE -" << std::endl;
std::cout << "1. Add element." << std::endl;
std::cout << "2. Find element." << std::endl;
std::cout << "3. Generate Hash." << std::endl;
std::cout << "4. Display Hash table." << std::endl;
std::cout << "5. Exit." << std::endl;
std::cin >> c;
switch (c) {
case 1:
std::cout << "Enter element to add = " << std::endl;
std::cin >> x;
h = mychain.hash(x);
h = std::abs(h);
mychain.add(x, h);
break;
case 2:
std::cout << "Enter element to search = " << std::endl;
std::cin >> x;
h = mychain.hash(x);
mychain.find(x, h);
break;
case 3:
std::cout << "Enter element to generate hash = " << std::endl;
std::cin >> x;
std::cout << "Hash of " << x << " is = " << mychain.hash(x)
<< std::endl;
break;
case 4:
mychain.display();
break;
default:
loop = false;
break;
}
std::cout << std::endl;
}
/*add(1,&head1);
add(2,&head1);
add(3,&head2);
add(5,&head1);
display(&head1);
display(&head2);*/
return 0;
}