0% found this document useful (0 votes)
4 views2 pages

Division Method

The document contains a C++ implementation of a division hashing algorithm using a hash table. It includes methods for inserting keys, handling collisions, and displaying the hash table contents. The program prompts the user for the size of the hash table and the keys to insert, demonstrating basic hash table functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Division Method

The document contains a C++ implementation of a division hashing algorithm using a hash table. It includes methods for inserting keys, handling collisions, and displaying the hash table contents. The program prompts the user for the size of the hash table and the keys to insert, demonstrating basic hash table functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

using namespace std;

class DivisionHashing {
private:
int* hashTable; // Array to store hash table values
int tableSize; // Size of the hash table

public:
// Constructor to initialize hash table with a given size
DivisionHashing(int size) {
tableSize = size;
hashTable = new int[tableSize]; // Dynamically allocate memory for the
array
for (int i = 0; i < tableSize; i++) {
hashTable[i] = -1; // Initialize all values to -1 (empty slots)
}
}

// Division hash function


int hash(int key) {
return key % tableSize; // Modulo operation to get index
}

// Insert a key into the hash table


void insert(int key) {
int index = hash(key); // Calculate the index using the division method
if (hashTable[index] == -1) {
hashTable[index] = key; // Insert the key if the index is empty
cout << "Inserted " << key << " at index " << index << endl;
} else {
cout << "Collision occurred at index " << index << ", cannot insert "
<< key << endl;
// This basic implementation doesn't handle collisions
}
}

// Display the hash table


void display() {
cout << "Hash Table:" << endl;
for (int i = 0; i < tableSize; i++) {
if (hashTable[i] != -1) {
cout << "Index " << i << ": " << hashTable[i] << endl;
} else {
cout << "Index " << i << ": Empty" << endl;
}
}
}

// Destructor to free dynamically allocated memory


~DivisionHashing() {
delete[] hashTable;
}
};

int main() {
int size, numKeys, key;

// Get the size of the hash table from the user


cout << "Enter the size of the hash table: ";
cin >> size;

// Create an object of DivisionHashing with the specified size


DivisionHashing dh(size);

// Get the number of keys to insert from the user


cout << "Enter the number of keys to insert: ";
cin >> numKeys;

// Insert keys into the hash table


for (int i = 0; i < numKeys; i++) {
cout << "Enter key " << i + 1 << ": ";
cin >> key;
dh.insert(key);
}

// Display the hash table contents


dh.display();
return 0;
}

You might also like