-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTL_HashTable.cpp
133 lines (112 loc) Β· 4.83 KB
/
STL_HashTable.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include<iostream>
#include <list>
using namespace std;
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
class Hashing{
int hash_bucket; // No. of buckets
list<int> *hashtable; // Pointer to an array containing buckets
public:
Hashing(int V); // Constructor
void insert_key(int val); // inserts a key into hash table
void delete_key(int key); // deletes a key from hash table
void displayHash();
int Search(int key);
// hash function to map values to key
int hashFunction(int x){
return (x % hash_bucket);
}
};
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
Hashing :: Hashing(int b){
this->hash_bucket = b;
hashtable = new list<int> [hash_bucket];
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
//insert to hash table
void Hashing :: insert_key(int key){
int index = hashFunction(key);
hashtable[index].push_back(key);
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
void Hashing :: delete_key(int key){
// get the hash index for key
int index = hashFunction(key);
// find the key in (i)th list
list <int> :: iterator i;
for (i = hashtable[index].begin();
i != hashtable[index].end(); i++){
if (*i == key)
break;
}
// if key is found in hash table, remove it
if (i != hashtable[index].end())
hashtable[index].erase(i);
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// display the hash table
void Hashing :: displayHash() {
for (int i = 0; i < hash_bucket; i++) {
cout << i;
for (int x : hashtable[i])
cout << " ββ> " << x;
cout << endl;
}
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
int Hashing :: Search(int key){
int i;
for (i = 0; i < hash_bucket; i++){
for (int x : hashtable[i])
if (x == key)
return i;
}
return 0;
}
/*βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/
// main program
int main() {
int num;
cout << "Enter the Number of Hash Buckets : ";
cin >> num;
cout << endl;
int ch, a, b, key;
cout << "1 βββββ> Insert \n";
cout << "2 βββββ> Delete \n";
cout << "3 βββββ> Display \n";
cout << "4 βββββ> Check if an Element is Present \n";
cout << "5 βββββ> Exit \n";
Hashing h(num);
do{
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch){
case (1):
cout << "Enter the value to be Inserted to the Hash Table : ";
cin >> a;
h.insert_key(a);
break;
case (2):
cout << "Enter the value to be Deleted from the Hash Table : ";
cin >> b;
h.delete_key(b);
break;
case (3):
cout << "\nHash Table : \n\n";
h.displayHash();
break;
case (4):
cout << "Enter the Element to be Checked : ";
cin >> key;
if (h.Search(key))
cout << key << " is present in the Hash Table in bucket " << h.Search(key) << endl;
else
cout << key << " is not present in the Hash Table\n";
break;
case (5):
break;
default:
cout << "Enter a value between 1 and 5";
}
}while(ch != 5);
return 0;
}