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

Folding Method

The document contains a C++ implementation of a Folding Hashing class, which includes methods for creating a hash table, inserting keys, and displaying the table's contents. The hash function divides keys into parts, sums them, and calculates an index for insertion, while handling collisions minimally. The program prompts the user for the hash table size, number of parts for keys, and the keys to insert, demonstrating the functionality of the hashing mechanism.
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)
5 views2 pages

Folding Method

The document contains a C++ implementation of a Folding Hashing class, which includes methods for creating a hash table, inserting keys, and displaying the table's contents. The hash function divides keys into parts, sums them, and calculates an index for insertion, while handling collisions minimally. The program prompts the user for the hash table size, number of parts for keys, and the keys to insert, demonstrating the functionality of the hashing mechanism.
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>
#include <vector>
using namespace std;

class FoldingHashing {
private:
int* hashTable; // Array to store hash table values
int tableSize; // Size of the hash table
int numParts; // Number of parts to divide the key into

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

// Folding hash function


int hash(int key) {
int sum = 0;
int partSize = pow(10, numParts); // Calculate the size of each part
cout<<"PartSize="<<partSize<<endl;

// Divide the key into parts and sum them


while (key > 0) {
sum += key % partSize; // Add the last 'numParts' digits to sum
key /= partSize; // Remove the last 'numParts' digits
}
cout<<"Sum of folded numbers are="<<sum<<endl;
return sum % tableSize; // Return the index within table size
}

// Insert a key into the hash table


void insert(int key) {
int index = hash(key); // Calculate the index using the folding 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


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

int main() {
int size, numParts, 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 parts to divide each key into


cout << "Enter the number of digits per part: ";
cin >> numParts;

// Create an object of FoldingHashing with the specified size and number of


parts
FoldingHashing fh(size, numParts);

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

// Display the hash table contents


fh.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