Skip to main content

Create a hash table and handle the collisions using Separate Chaining.

CODE:


 #include <iostream>

#include <list>

#include <vector>


using namespace std;


class HashTable {

private:

    int size;

    vector<list<int>> table;


public:

    HashTable(int s) : size(s) {

        table.resize(size);

    }


    // Hash function to map values to keys

    int hash(int key) {

        return key % size;

    }


    // Function to insert key into hash table

    void insert(int key) {

        int index = hash(key);

        table[index].push_back(key);

    }


    // Function to search for a key in the hash table

    bool search(int key) {

        int index = hash(key);

        for (auto it = table[index].begin(); it != table[index].end(); ++it) {

            if (*it == key)

                return true;

        }

        return false;

    }


    // Function to display the hash table

    void display() {

        for (int i = 0; i < size; ++i) {

            cout << "[" << i << "]";

            for (auto it = table[i].begin(); it != table[i].end(); ++it) {

                cout << " --> " << *it;

            }

            cout << endl;

        }

    }

};


int main() {

    HashTable ht(10); // Hash table of size 10


    // Insert some keys

    ht.insert(5);

    ht.insert(15);

    ht.insert(25);

    ht.insert(35);

    ht.insert(45);


    // Display the hash table

    ht.display();


    // Search for a key

    int searchKey = 15;

    if (ht.search(searchKey))

        cout << "Key " << searchKey << " found in the hash table." << endl;

    else

        cout << "Key " << searchKey << " not found in the hash table." << endl;


    return 0;

}










Popular posts from this blog

krushnaaa

  import java . awt .* ; import java . awt . event .* ; public class SimpleAWTExample {     public static void main ( String [] args ) {         // Create a Frame         Frame frame = new Frame ( "Simple AWT Example" );         // Create a Button         Button button = new Button ( "Click Me!" );         // Set layout for the Frame         frame . setLayout ( new FlowLayout ());         // Add the Button to the Frame         frame . add ( button );         // Set size of the Frame         frame . setSize ( 300 , 200 ); // Width: 300 pixels, Height: 200 pixels         // Make the Frame visible         frame . setVisible ( true );         // Add a WindowListener to handle closing event     ...

Write a program to create a class Student2 along with two method getData (), printData () to get the value through argument and display the data in printData. Create the two objects s1, s2 to declare and access the values from class STtest.

 CODE: import java.util.Scanner; class Student2 {     private String name;     private int age;          // Method to set data     public void getData(String name, int age) {         this.name = name;         this.age = age;     }          // Method to print data     public void printData() {         System.out.println("Name: " + name);         System.out.println("Age: " + age);     } } public class STtest {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);                  // Creating objects of Student2 class         Student2 s1 = new Student2();         Student2 s2 = new Student2();              ...

Create an in-order threaded binary search tree and perform the traversals.(Double Thread)

 CODE: #include <iostream> using namespace std; struct Node {     int data;     Node* left;     Node* right;     bool leftThread;     bool rightThread; }; Node* createThreadedBST(int arr[], int n) {     Node* root = nullptr;     for (int i = 0; i < n; ++i) {         Node* newNode = new Node;         newNode->data = arr[i];         newNode->left = newNode->right = nullptr;         newNode->leftThread = newNode->rightThread = true;         if (root == nullptr) {             root = newNode;         } else {             Node* current = root;             Node* parent = nullptr;             while (true) {                ...