A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d9/d92/chaining_8cpp_source.html below:

TheAlgorithms/C++: hashing/chaining.cpp Source File

Go to the documentation of this file. 23

std::shared_ptr<struct Node> next;

26

std::vector<std::shared_ptr<Node>>

head

;

36 while

(mod--)

head

.push_back(

nullptr

);

45 void add

(

int

x,

int h

) {

46

std::shared_ptr<Node> curr;

47

std::shared_ptr<Node> temp(

new Node

);

49

temp->next =

nullptr

;

55 while

(curr->next) curr = curr->next;

64

std::shared_ptr<Node> temp =

nullptr

;

66 for

(i = 0; i <

_mod

; i++) {

68

std::cout <<

"Key "

<< i <<

" is empty"

<< std::endl;

70

std::cout <<

"Key "

<< i <<

" has values = "

<< std::endl;

73

std::cout << temp->data <<

" "

<< std::endl;

76

std::cout << temp->data;

77

std::cout << std::endl;

91 virtual int hash

(

int

x)

const

{

return

x %

_mod

; }

102

std::shared_ptr<Node> temp =

head

[

h

];

105

std::cout <<

"Element not found"

<< std::endl;

110 while

(temp->data != x && temp->next) temp = temp->next;

113

std::cout <<

"Element found"

<< std::endl;

119 if

(temp->data == x) {

120

std::cout <<

"Element found"

<< std::endl;

125

std::cout <<

"Element not found"

<< std::endl;

134 int

c = 0, x = 0, mod = 0,

h

= 0;

135

std::cout <<

"Enter the size of Hash Table. = "

<< std::endl;

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;

152

std::cout <<

"Enter element to add = "

<< std::endl;

154 h

= mychain.

hash

(x);

156

mychain.

add

(x,

h

);

159

std::cout <<

"Enter element to search = "

<< std::endl;

161 h

= mychain.

hash

(x);

162

mychain.

find

(x,

h

);

165

std::cout <<

"Enter element to generate hash = "

<< std::endl;

167

std::cout <<

"Hash of "

<< x <<

" is = "

<< mychain.

hash

(x)

177

std::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