forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hashing_with_chaining.cpp
85 lines (71 loc) · 2.1 KB
/
Hashing_with_chaining.cpp
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
/**
* @author omkarlanghe : https://github.com/omkarlanghe
* A program to demostrate basic working of a data structure called Hashing.
* This program supports seperate chaining method to avoid collision between hash indexes.
*/
#include <iostream>
#include <list>
class HashTable {
private:
int BUCKET;
std::list<int> *table = nullptr;
public:
HashTable(int v);
void insert_item(int x);
void delete_item(int key);
int hash_function(int data) { return (data % BUCKET); }
void display_hash_table();
};
/** constructor function which initialized hash table */
HashTable::HashTable(int b) {
this->BUCKET = b;
table = new std::list<int>[BUCKET];
}
/** function to insert item in a hash table */
void HashTable::insert_item(int key) {
int index = hash_function(key);
table[index].push_back(key);
}
/** function to delete item from a hash table */
void HashTable::delete_item(int key) {
int index = hash_function(key);
std::list<int>::iterator it;
for (it = table[index].begin() ; it != table[index].end() ; it++) {
if (*it == key) {
break;
}
}
if (it != table[index].end()) {
table[index].erase(it);
}
}
/** function to display all the elements from hash table */
void HashTable::display_hash_table() {
for (int i = 0 ; i < BUCKET; i++) {
std::cout << i;
for (auto x : table[i]) {
std::cout << " :: " << x;
}
std::cout << std::endl;
}
}
/** main function */
int main() {
int size, x;
std::cout << "Enter the size : " << std::endl;
std::cin >> size;
HashTable hash(size);
std::cout << "Enter the elements in hash table : " << std::endl;
for (int i = 0 ; i < size ; i++) {
std::cin >> x;
hash.insert_item(x);
}
std::cout << "Display before deletion : " << std::endl;
hash.display_hash_table();
std::cout << "Enter the element to remove : " << std::endl;
std::cin >> x;
hash.delete_item(x);
std::cout << "Display after deletion : " << std::endl;
hash.display_hash_table();
return (0);
}