Skip to main content

Create a hash table and handle the collisions using linear probing with replacement.

CODE:


 #include <iostream>

#include <vector>

#include <string>


using namespace std;


// Define a class for the hash table

class HashTable {

private:

    vector<pair<string, int>> table;

    int capacity;

    int size;


    // Hash function

    int hash(const string& key) {

        int hash = 0;

        for (char c : key) {

            hash += c;

        }

        return hash % capacity;

    }


    // Helper function to find the next available slot using linear probing

    int findNextAvailable(int index) {

        int i = index + 1;

        while (table[i].first != "") {

            i = (i + 1) % capacity;

        }

        return i;

    }


public:

    // Constructor

    HashTable(int capacity) : capacity(capacity), size(0) {

        table.resize(capacity);

    }


    // Function to insert a key-value pair into the hash table

    void insert(const string& key, int value) {

        int index = hash(key);

        if (table[index].first == "") {

            table[index] = make_pair(key, value);

            size++;

        } else {

            // Handle collision using linear probing with replacement

            int nextAvailable = findNextAvailable(index);

            table[nextAvailable] = make_pair(key, value);

            size++;

        }

    }


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

    int search(const string& key) {

        int index = hash(key);

        int originalIndex = index;

        while (table[index].first != key && table[index].first != "") {

            index = (index + 1) % capacity;

            if (index == originalIndex) {

                return -1; // Key not found

            }

        }

        if (table[index].first == key) {

            return table[index].second;

        } else {

            return -1; // Key not found

        }

    }

};


int main() {

    // Create a hash table with capacity 10

    HashTable ht(10);


    // Insert some key-value pairs

    ht.insert("John", 25);

    ht.insert("Emma", 30);

    ht.insert("Bob", 35);


    // Search for keys

    cout << "Age of John: " << ht.search("John") << endl;

    cout << "Age of Emma: " << ht.search("Emma") << endl;

    cout << "Age of Bob: " << ht.search("Bob") << endl;

    cout << "Age of Alice: " << ht.search("Alice") << endl; // Key not found


    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) {                ...