std::shared_ptr<struct Node> next;
26std::vector<std::shared_ptr<Node>>
head;
36 while(mod--)
head.push_back(
nullptr);
45 void add(
intx,
int h) {
46std::shared_ptr<Node> curr;
47std::shared_ptr<Node> temp(
new Node);
49temp->next =
nullptr;
55 while(curr->next) curr = curr->next;
64std::shared_ptr<Node> temp =
nullptr;
66 for(i = 0; i <
_mod; i++) {
68std::cout <<
"Key "<< i <<
" is empty"<< std::endl;
70std::cout <<
"Key "<< i <<
" has values = "<< std::endl;
73std::cout << temp->data <<
" "<< std::endl;
76std::cout << temp->data;
77std::cout << std::endl;
91 virtual int hash(
intx)
const{
returnx %
_mod; }
102std::shared_ptr<Node> temp =
head[
h];
105std::cout <<
"Element not found"<< std::endl;
110 while(temp->data != x && temp->next) temp = temp->next;
113std::cout <<
"Element found"<< std::endl;
119 if(temp->data == x) {
120std::cout <<
"Element found"<< std::endl;
125std::cout <<
"Element not found"<< std::endl;
134 intc = 0, x = 0, mod = 0,
h= 0;
135std::cout <<
"Enter the size of Hash Table. = "<< std::endl;
142std::cout << std::endl;
143std::cout <<
"PLEASE CHOOSE -"<< std::endl;
144std::cout <<
"1. Add element."<< std::endl;
145std::cout <<
"2. Find element."<< std::endl;
146std::cout <<
"3. Generate Hash."<< std::endl;
147std::cout <<
"4. Display Hash table."<< std::endl;
148std::cout <<
"5. Exit."<< std::endl;
152std::cout <<
"Enter element to add = "<< std::endl;
154 h= mychain.
hash(x);
156mychain.
add(x,
h);
159std::cout <<
"Enter element to search = "<< std::endl;
161 h= mychain.
hash(x);
162mychain.
find(x,
h);
165std::cout <<
"Enter element to generate hash = "<< std::endl;
167std::cout <<
"Hash of "<< x <<
" is = "<< mychain.
hash(x)
177std::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.
struct Node { int data{}; std::shared_ptr< struct Node > next; } Node
Define a linked node.
std::vector< std::shared_ptr< Node > > head
array of nodes
int _mod
modulus of the class
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4