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

Digit Extraction

The document contains a C++ program that implements a digit extraction hashing class for storing keys in a hash table. It allows users to specify the size of the hash table and the number of digits to extract from each key for hashing. The program includes methods for inserting keys and displaying the hash table, while also handling basic collision detection.
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)
8 views2 pages

Digit Extraction

The document contains a C++ program that implements a digit extraction hashing class for storing keys in a hash table. It allows users to specify the size of the hash table and the number of digits to extract from each key for hashing. The program includes methods for inserting keys and displaying the hash table, while also handling basic collision detection.
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>

#include <cmath>
using namespace std;

class DigitExtractionHashing {
private:
int* hashTable; // Array to store hash table values
int tableSize; // Size of the hash table
int numDigits; // Number of digits to extract from the key

public:
// Constructor to initialize hash table with a given size
DigitExtractionHashing(int size, int digits) {
tableSize = size;
numDigits = digits;
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)
}
}

// Digit extraction hash function


int hash(int key) {
// Extract last 'numDigits' digits of the key
int divisor = pow(10, numDigits); // Calculate divisor to get the last
'numDigits' digits
int extractedDigits = key % divisor; // Extract the last 'numDigits'
digits
return extractedDigits % tableSize; // Apply modulo operation to get index
within table size
}

// Insert a key into the hash table


void insert(int key) {
int index = hash(key); // Calculate the index using the digit extraction
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
~DigitExtractionHashing() {
delete[] hashTable;
}
};

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

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


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

// Get the number of digits to extract from the user


cout << "Enter the number of digits to extract from the key: ";
cin >> numDigits;

// Create an object of DigitExtractionHashing with the specified size and


number of digits to extract
DigitExtractionHashing deh(size, numDigits);

// 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;
deh.insert(key);
}

// Display the hash table contents


deh.display();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy