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
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...
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
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
The goal of this assignment is to implement a set container using linked lists. Use the...
The goal of this assignment is to implement a set container using linked lists. Use the authors bag3.h and bag3.cpp as a basis for implementing your set container using linked lists. The authors bag3.h and bag3.cpp can be found here https://www.cs.colorado.edu/~main/chapter5/ Since you are using the authors bag3.h and bag3.cpp for your Set container implementation, make sure that you change the name of the class and constructors to reflect the set class. Additionally you will need to implement the follow...
Please include comments on what you are doing.   Using linked lists, write a Python program that...
Please include comments on what you are doing.   Using linked lists, write a Python program that performs the following tasks: store the records for each college found in the input file - colleges.csv - into a linked list. (File includes name and state data fields) allow the user to search the linked list for a college’s name; display a message indicating whether or not the college’s name was in the database allow the user to enter a state's name and...
This is c++ Create a program where you create, display, search, delete elements within a linked...
This is c++ Create a program where you create, display, search, delete elements within a linked list. Watch your function arguments. Pointer parameters are passed by reference to some functions and by value to others. Functions to make: copy : copies element in linked list destroy all: destroys all elements in linked list wherethisgoes:user  enters element and returns where the element is located insert sorted: inserts element create using linked lists with a head pointer and so forth
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT