Professional Documents
Culture Documents
351 HashTableClassChainingC++
351 HashTableClassChainingC++
class Node{
public:
int data;
Node* next;
};
// Hash Table
class HashTable{
public:
Node** HT;
HashTable();
int hash(int key);
void Insert(int key);
int Search(int key);
~HashTable();
};
HashTable::HashTable() {
HT = new Node* [10];
for (int i=0; i<10; i++){
HT[i] = nullptr;
}
}
HashTable::~HashTable() {
for (int i=0; i<10; i++){
Node* p = HT[i];
while (HT[i]){
HT[i] = HT[i]->next;
delete p;
p = HT[i];
}
}
delete [] HT;
}
int main() {
int A[] = {16, 12, 25, 39, 6, 122, 5, 68, 75};
int n = sizeof(A)/sizeof(A[0]);
HashTable H;
for (int i=0; i<n; i++){
H.Insert(A[i]);
}
cout << "Successful Search" << endl;
int key = 6;
int value = H.Search(key);
cout << "Key: " << key << ", Value: " << value << endl;
cout << "Unsuccessful Search" << endl;
key = 95;
value = H.Search(key);
cout << "Key: " << key << ", Value: " << value << endl;
return 0;
}