Insert - Deletion in Hash Table
Insert - Deletion in Hash Table
#include <list>
class HashTable{
private:
list<int> *table;
int total_elements;
public:
// Constructor to create a hash table with 'n' indices:
HashTable(int n){
total_elements = n;
table = new list<int>[total_elements];
}
list<int>::iterator i;
for (i = table[x].begin(); i != table[x].end(); i++) {
// Check if the iterator points to the required item:
if (*i == key)
break;
}
void printAll(){
// Traverse each index:
for(int i = 0; i < total_elements; i++){
cout << "Index " << i << ": ";
// Traverse the list at current index:
for(int j : table[i])
cout << j << " => ";
int main() {
// Create a hash table with 3 indices:
HashTable ht(3);
// Declare the data to be stored in the hash table:
int arr[] = {2, 4, 6, 8, 10};
ht.removeElement(4);
cout << endl << "..:: After deleting 4 ::.." << endl;
ht.printAll();
return 0;
}
---------OUTPUT---------
..:: Hash Table ::..
Index 0: 6 =>
Index 1: 4 => 10 =>
Index 2: 2 => 8 =>