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,20 +1,54 @@
#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
public:
/**
* @brief Construct a new chain object
*
* @param mod modulus of the chain
*/
chain(int mod) : _mod(mod) {
while (mod--) head.push_back(nullptr);
} }
/**
* @brief create and add a new node with a give value and at a given height
*
* @param x value at the new node
* @param h height of the node
*/
void add(int x, int h) { void add(int x, int h) {
struct Node *temp = new Node; std::shared_ptr<struct Node> curr;
std::shared_ptr<struct Node> temp(new struct Node);
temp->data = x; temp->data = x;
temp->next = NULL; temp->next = nullptr;
if (!head[h]) { if (!head[h]) {
head[h] = temp; head[h] = temp;
curr = head[h]; curr = head[h];
@ -25,86 +59,104 @@ void add(int x, int h) {
} }
} }
void display(int mod) { /**
struct Node *temp; * @brief Display the chain
int i; */
for (i = 0; i < mod; i++) { void display() {
std::shared_ptr<struct Node> temp = nullptr;
int i = 0;
for (i = 0; i < _mod; i++) {
if (!head[i]) { if (!head[i]) {
cout << "Key " << i << " is empty" << endl; std::cout << "Key " << i << " is empty" << std::endl;
} else { } else {
cout << "Key " << i << " has values = "; std::cout << "Key " << i << " has values = ";
temp = head[i]; temp = head[i];
while (temp->next) { while (temp->next) {
cout << temp->data << " "; std::cout << temp->data << " ";
temp = temp->next; temp = temp->next;
} }
cout << temp->data; std::cout << temp->data;
cout << endl; std::cout << std::endl;
} }
} }
} }
int hash(int x, int mod) { return x % mod; } /**
* @brief Compute the hash of a value for current chain
*
* @param x value to compute modulus of
* @return modulus of `x`
*/
int hash(int x) const { return x % _mod; }
void find(int x, int h) { void find(int x, int h) {
struct Node *temp = head[h]; auto temp = head[h];
if (!head[h]) { if (!head[h]) {
cout << "Element not found"; std::cout << "Element not found";
return; return;
} }
while (temp->data != x && temp->next) temp = temp->next; while (temp->data != x && temp->next) temp = temp->next;
if (temp->next) if (temp->next) {
cout << "Element found"; std::cout << "Element found";
else { } else {
if (temp->data == x) if (temp->data == x) {
cout << "Element found"; std::cout << "Element found";
else } else {
cout << "Element not found"; 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);
int main(void) {
init();
int c, x, mod, h;
cout << "Enter the size of Hash Table. = ";
cin >> 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);