fix chaining

This commit is contained in:
Krishna Vedala 2020-07-23 17:25:44 -04:00
parent 1e648fc1d2
commit 7ae91d2607
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -1,110 +1,162 @@
#include <math.h> /**
* @file chaining.cpp
* @author [vasutomar](https://github.com/vasutomar)
* @author [Krishna Vedala](https://github.com/kvedala)
* @brief
*/
#include <cmath>
#include <iostream> #include <iostream>
using namespace std; #include <memory>
#include <vector>
namespace { // keep the code local to this file by assigning them to an unnamed
// namespace
/**
* @{
* @brief Define a linked node
*/
struct Node { struct Node {
int data; int data{}; ///< data stored in the node
struct Node *next; std::shared_ptr<struct Node> next; ///< pointer to the next node
} * head[100], *curr; };
void init() { /**
for (int i = 0; i < 100; i++) head[i] = NULL; * @brief Chain class with a given modulus
} */
class chain {
private:
std::vector<std::shared_ptr<struct Node>> head; ///< array of nodes
int _mod; ///< modulus of the class
void add(int x, int h) { public:
struct Node *temp = new Node; /**
temp->data = x; * @brief Construct a new chain object
temp->next = NULL; *
if (!head[h]) { * @param mod modulus of the chain
head[h] = temp; */
curr = head[h]; chain(int mod) : _mod(mod) {
} else { while (mod--) head.push_back(nullptr);
curr = head[h];
while (curr->next) curr = curr->next;
curr->next = temp;
} }
}
void display(int mod) { /**
struct Node *temp; * @brief create and add a new node with a give value and at a given height
int i; *
for (i = 0; i < mod; i++) { * @param x value at the new node
if (!head[i]) { * @param h height of the node
cout << "Key " << i << " is empty" << endl; */
void add(int x, int h) {
std::shared_ptr<struct Node> curr;
std::shared_ptr<struct Node> temp(new struct Node);
temp->data = x;
temp->next = nullptr;
if (!head[h]) {
head[h] = temp;
curr = head[h];
} else { } else {
cout << "Key " << i << " has values = "; curr = head[h];
temp = head[i]; while (curr->next) curr = curr->next;
while (temp->next) { curr->next = temp;
cout << temp->data << " ";
temp = temp->next;
}
cout << temp->data;
cout << endl;
} }
} }
}
int hash(int x, int mod) { return x % mod; } /**
* @brief Display the chain
void find(int x, int h) { */
struct Node *temp = head[h]; void display() {
if (!head[h]) { std::shared_ptr<struct Node> temp = nullptr;
cout << "Element not found"; int i = 0;
return; for (i = 0; i < _mod; i++) {
if (!head[i]) {
std::cout << "Key " << i << " is empty" << std::endl;
} else {
std::cout << "Key " << i << " has values = ";
temp = head[i];
while (temp->next) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << temp->data;
std::cout << std::endl;
}
}
} }
while (temp->data != x && temp->next) temp = temp->next;
if (temp->next)
cout << "Element found";
else {
if (temp->data == x)
cout << "Element found";
else
cout << "Element not found";
}
}
int main(void) { /**
init(); * @brief Compute the hash of a value for current chain
int c, x, mod, h; *
cout << "Enter the size of Hash Table. = "; * @param x value to compute modulus of
cin >> mod; * @return modulus of `x`
*/
int hash(int x) const { return x % _mod; }
void find(int x, int h) {
auto temp = head[h];
if (!head[h]) {
std::cout << "Element not found";
return;
}
while (temp->data != x && temp->next) temp = temp->next;
if (temp->next) {
std::cout << "Element found";
} else {
if (temp->data == x) {
std::cout << "Element found";
} else {
std::cout << "Element not found";
}
}
}
};
} // namespace
/** Main function
* @returns `0` always
*/
int main() {
int c = 0, x = 0, mod = 0, h = 0;
std::cout << "Enter the size of Hash Table. = ";
std::cin >> mod;
chain mychain(mod);
bool loop = true; bool loop = true;
while (loop) { while (loop) {
cout << endl; std::cout << std::endl;
cout << "PLEASE CHOOSE -" << endl; std::cout << "PLEASE CHOOSE -" << std::endl;
cout << "1. Add element." << endl; std::cout << "1. Add element." << std::endl;
cout << "2. Find element." << endl; std::cout << "2. Find element." << std::endl;
cout << "3. Generate Hash." << endl; std::cout << "3. Generate Hash." << std::endl;
cout << "4. Display Hash table." << endl; std::cout << "4. Display Hash table." << std::endl;
cout << "5. Exit." << endl; std::cout << "5. Exit." << std::endl;
cin >> c; std::cin >> c;
switch (c) { switch (c) {
case 1: case 1:
cout << "Enter element to add = "; std::cout << "Enter element to add = ";
cin >> x; std::cin >> x;
h = hash(x, mod); h = mychain.hash(x);
h = fabs(h); h = std::fabs(h);
add(x, h); mychain.add(x, h);
break; break;
case 2: case 2:
cout << "Enter element to search = "; std::cout << "Enter element to search = ";
cin >> x; std::cin >> x;
h = hash(x, mod); h = mychain.hash(x);
find(x, h); mychain.find(x, h);
break; break;
case 3: case 3:
cout << "Enter element to generate hash = "; std::cout << "Enter element to generate hash = ";
cin >> x; std::cin >> x;
cout << "Hash of " << x << " is = " << hash(x, mod); std::cout << "Hash of " << x << " is = " << mychain.hash(x);
break; break;
case 4: case 4:
display(mod); mychain.display();
break; break;
default: default:
loop = false; loop = false;
break; break;
} }
cout << endl; std::cout << std::endl;
} }
/*add(1,&head1); /*add(1,&head1);
add(2,&head1); add(2,&head1);