Question

In: Computer Science

In C++ Use vectors instead of linked lists Create a Hash table program using H(key) =...

In C++

Use vectors instead of linked lists

Create a Hash table program using H(key) = key%tablesize with Chaining and Linear probing for a text file that has a list of 50 numbers

Ask the user to enter the file name, what the table size is, and which of the two options they want to use between chaining and linear probing

Solutions

Expert Solution

Code in C++

#include<bits/stdc++.h>
using namespace std;

int hashValue(int key, int tableSize)
{
    return key % tableSize;
}
int main()
{
    string filename;
    cin>>filename;
    
    int tableSize, choice;
    cin>>tableSize>>choice;

    freopen(filename.c_str(), "r", stdin);  //open file to read 50 number
    int n;
    cin>>n;
    int num[n];
    for(int i=0;i<n;i++)    cin>>num[i];

    vector<int>hashTable1[tableSize];
    vector<int>hashTable2(tableSize, -1);           // -1 means unoccupied
    // use switch case
    switch (choice)
    {
    //for chaining
    case 1:
        //use array of vectors, here vector (instead of linkedlist) use for chaining
        

        //insert into hashtable
        for(int i=0;i<n;i++)
        {
            int hashed = hashValue(num[i], tableSize);
            hashTable1[hashed].push_back(num[i]);
        }
        cout<<"Hash table:\n";
        for(int i=0;i<tableSize;i++)
        {
            cout<<i<<" -> ";
            for(int j=0;j<hashTable1[i].size();j++)
                cout<<hashTable1[i][j]<<" ";
            cout<<"\n";
        }
        break;

    //for linear probe
    case 2:
  
        for(int i=0;i<n;i++)
        {
            int hashed = hashValue(num[i], tableSize);
            while(hashTable2[hashed] != -1)
            {
                hashed = (hashed + 1)%tableSize;
            }
            hashTable2[hashed] = num[i];
        }

        cout<<"Hash Table:\n";
        for(int i=0;i<tableSize;i++)
            cout<<i<<" -> "<<hashTable2[i]<<"\n";

        break;
    default:
        cout<<"Enter correct choice!";
        break;
    }

}

Input file

input & output


Related Solutions

Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
Use double hashing and the following hash functions: hash(key) = (key % table size) and hash2(key)...
Use double hashing and the following hash functions: hash(key) = (key % table size) and hash2(key) = (16 - (key % 10)) + 1 to insert the following numbers into a dictionary's hash table: 1, 12, 14, 3, 5 and 17. The table size is 11. Show the table and where each value is inserted. No coding required for this problem.
SHOW WORK Draw the hash table that results using the hash function: h(k)=kmod7 to hash the...
SHOW WORK Draw the hash table that results using the hash function: h(k)=kmod7 to hash the keys 41, 16, 40, 47, 10, 55. Assuming collisions are handled by Double hashing. SHOW WORK
3. Draw the hash table that results using the hash function: h(k)=kmod 7 to hash the...
3. Draw the hash table that results using the hash function: h(k)=kmod 7 to hash the keys 50, 700, 76, 85, 92, 73, 101. Assuming collisions are handled by Quadratic probing. Don't write a program. Just please manually solve the problem. Thanks.
Can you write a program for the card game WAR using linked lists in c++!
Can you write a program for the card game WAR using linked lists in c++!
Can you write a program for snakes and ladder board game using linked lists in c++
Can you write a program for snakes and ladder board game using linked lists in c++
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game is played with between 2 and 4 players. You'll have a deck of cards containing some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten. When that happens, that person explodes. They are now dead and out of the game. This process continues until there's only one player left, who...
Topic: Students will be able to create skills in the use of linked lists, the stack,...
Topic: Students will be able to create skills in the use of linked lists, the stack, and the queue abstract data types, by implementing solutions to fundamental data structures and associated problems. Add the code for the methods where it says to implement. The main class is already done. There is a sample of the output. 1. A double-ended queue, or deque, is a data structure consisting of a list of items on which the following operations are defined: addToBack(x):...
C# Programming create a Hash Function
C# Programming create a Hash Function
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT