Here's an example implementation of a HashTable demo using the provided instructions:
```c++
#include <iostream>
#include <string>
const int TABLE_SIZE = 21;
struct Node {
std::string fruit;
int cnt;
};
int hashFunction(const std::string& fruit) {
// Simple hash function that sums the ASCII values of the characters
int sum = 0;
for (char c : fruit) {
sum += c;
}
return sum % TABLE_SIZE;
}
void displayHashTable(const Node Fruit[]) {
for (int i = 0; i < TABLE_SIZE; i++) {
std::cout << "Bucket " << i << ": ";
if (Fruit[i].fruit.empty()) {
std::cout << "Empty" << std::endl;
} else {
std::cout << Fruit[i].fruit << " (" << Fruit[i].cnt << ")" << std::endl;
}
}
}
int main() {
Node Fruit[TABLE_SIZE] = {}; // Initialize all nodes to empty
// Initialize the array with fruit names and cnt values
std::string fruitNames[TABLE_SIZE] = {
"Apple(1)", "Banana(2)", "Cherry(3)", "Date(4)", "Eldenberry(5)", "Fig(6)",
"Guava(7)", "Honeydew(8)", "Jackfruit(9)", "Kiwi(10)", "Lemon(11)", "Mango(12)",
"Nut(13)", "Orange(14)", "Pear(15)", "Quince(16)", "Raisin(17)", "Strawberry(18)",
"Tangerine(19)", "Watermelon(20)", "Yumberry(21)"
};
for (int i = 0; i < TABLE_SIZE; i++) {
int hash = hashFunction(fruitNames[i]);
Fruit[hash].fruit = fruitNames[i];
Fruit[hash].cnt = i + 1;
}
displayHashTable(Fruit);
return 0;
}
```
In this implementation, the `Node` struct template is used to create nodes that store a fruit name and a cnt value. The `hashFunction()` function is a simple hash function that sums the ASCII values of the characters in the fruit name and returns the hash value modulo the table size. The `displayHashTable()` function is used to print the contents of the HashTable.
The main function initializes the `Fruit` array with the provided fruit names and cnt values. It calculates the hash value for each fruit name using the `hashFunction()` and stores the fruit name and cnt value in the corresponding bucket of the `Fruit` array.
Finally, the `displayHashTable()` function is called to display the contents of the HashTable.
Note: This implementation uses a simple hash function for demonstration purposes. In practice, more sophisticated hash functions may be used to achieve better distribution and reduce collisions.