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

Linear Probing

This document contains a C++ implementation of a Linear Probing Hash Table. It includes methods for initializing the table, inserting keys, displaying the table contents, and managing memory with a destructor. The main function allows user input for the size of the hash table and the keys to be inserted.
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)
3 views2 pages

Linear Probing

This document contains a C++ implementation of a Linear Probing Hash Table. It includes methods for initializing the table, inserting keys, displaying the table contents, and managing memory with a destructor. The main function allows user input for the size of the hash table and the keys to be inserted.
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 LinearProbingHashTable {
private:
int* table; // Hash table array
int tableSize; // Size of the hash table
int numElements; // Number of elements in the hash table

public:
// Constructor to initialize the hash table with the given size
LinearProbingHashTable(int size) {
tableSize = size;
table = new int[tableSize];
for (int i = 0; i < tableSize; i++) {
table[i] = -1; // Initialize the table with -1 (empty slots)
}
numElements = 0;
}

// Hash function using the division method


int hash(int key) {
return key % tableSize;
}

// Insert a key into the hash table


void insert(int key) {
if (numElements == tableSize) {
cout << "Hash table is full, cannot insert key " << key << endl;
return;
}

int index = hash(key);

// Linear probing: Find the next available slot


while (table[index] != -1) {
index = (index + 1) % tableSize; // Wrap around to the beginning if
necessary
}

table[index] = key;
numElements++;
cout << "Inserted key " << key << " at index " << index << endl;
}

// Display the hash table


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

// Destructor to clean up the dynamically allocated memory


~LinearProbingHashTable() {
delete[] table;
}
};

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 LinearProbingHashTable


LinearProbingHashTable hashTable(size);

// Insert keys into the hash table


cout << "Enter the number of keys to insert: ";
cin >> numKeys;
for (int i = 0; i < numKeys; i++) {
cout << "Enter key " << i + 1 << ": ";
cin >> key;
hashTable.insert(key);
}

// Display the hash table contents


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