Question

C++: Create a class called "HashTable" This class will implement hashing on an array of integers...

C++:

Create a class called "HashTable"

This class will implement hashing on an array of integers

Make the table size 11

Implement with linear probing. The hash function should be: h(x) = x % 11

Create search(), insert() and delete() member functions. Search should return the index of where the target is located, insert should allow the user to insert a value, and delete removes the desired value from the table.

In main perform the following:

1. Insert: 17 11 5 20 10 22 33 45

2. Print out the contents of the hash table (hint: I would make a member function for this)

3. Search for 11, then search for 45 and then search for 26

4. Delete 11

5. Search for 33

Every time you search for something, output it to the console.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Code :

#include <iostream>

using namespace std;

class HashTable{
public:
int hash[11]={0,0,0,0,0,0,0,0,0,0,0};
int hashFunction(int a){
return a%11;
}
int search(int a){
for(int i=0;i<11;i++){
if(a== hash[i]){
return i;
}
}
return -1;
}
  
void insert(int index, int value){
int i=0;
if(hash[index]==0){
hash[index] = value;
}
else {
while(i<11){
index++;
if(hash[index%11] == 0){
hash[index]=value;
return;
}
i++;
}
}
  
}
void deleteValue(int value){
for(int i=0;i<11;i++){
if(hash[i] == value){
hash[i] = 0;
cout << "value deleted :";
cout << value ;
cout << "\n";
break;
}
}
  
}
  
};

int main()
{
HashTable ht;
ht.insert(ht.hashFunction(17),17);
ht.insert(ht.hashFunction(11),11);
ht.insert(ht.hashFunction(5),5);
ht.insert(ht.hashFunction(20),20);
ht.insert(ht.hashFunction(10),10);
ht.insert(ht.hashFunction(22),22);
ht.insert(ht.hashFunction(33),33);
ht.insert(ht.hashFunction(45),45);
for(int i=0;i<11;i++){
cout << ht.hash[i];
cout << " ";
}
cout << "\n";
cout << "Found at Index :";
cout << ht.search(11);
cout << "\nFound at Index :";
cout << ht.search(45);
cout << "\nFound at Index :";
cout << ht.search(26);
cout << "\n";
ht.deleteValue(11);
cout << "\nFound at Index :";
cout << ht.search(33);
cout << "\n";
return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++: Create a class called "HashTable" This class will implement hashing on an array of integers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • To implement a HashTable class in C++ that allows: - the definition of the size (M) of the static...

    To implement a HashTable class in C++ that allows: - the definition of the size (M) of the static array for the hash table - the definition of a hash function HASH(X) suitable for storing string keys (not integers). The student is expected to propose his/her own hash table. - a method for inserting keys in the hash table. The solution MUST consider chaining (e.g. linked lists, trees, etc.) as the collision response criteria. - a method for searching keys...

  • Title: Implementation of chained hashing in Java or Python Description: Implement a double linked list with...

    Title: Implementation of chained hashing in Java or Python Description: Implement a double linked list with a procedure for adding list elements. Implement a hash table using chaining and the linked list discussed above. Use Array size m = 7 A hash function of: h = k mod 7 Insert 100 random key values into the table. Key values should be between 0 and 99. Implement a Search procedure that tells whether a particular key value is in the hash...

  • Implement a StringHash class using python using open addressing and linear probing, it should contain the following methods: Using strings for the data 1. StringHash(size) — create a new hashTable wit...

    Implement a StringHash class using python using open addressing and linear probing, it should contain the following methods: Using strings for the data 1. StringHash(size) — create a new hashTable with a default size of 11 for your array, but allow the user to specify an alternate size if desired 2. addItem(string) — place value into the hashTable iv. bool PindItem(string value) — return true if the value is present, else false 3. findItem(string value) — return true if the...

  • C programming Let's try to use the template below to implement a Set with double hashing....

    C programming Let's try to use the template below to implement a Set with double hashing. Here we assume the Set contains names with 3 characters long. Since it is a Set, we do not need to have an explicit value for each key. We will use a token value of 1 if a name belongs to the Set. Let's reuse the exact hash functions we have seen in example in Part 7 of the lecture notes, page 3. As...

  • C programming Problem 3 [Set via Hashing] As mentioned in the lecture, a hash table can...

    C programming Problem 3 [Set via Hashing] As mentioned in the lecture, a hash table can be used to implement a Set ADT. Let's try to use the template below to implement a Set with double hashing. Here we assume the Set contains names with 3 characters long. Since it is a Set, we do not need to have an explicit value for each key. We will use a token value of 1 if a name belongs to the Set....

  • in C++ Code should work for all cases In this assignment you are requested to implement...

    in C++ Code should work for all cases In this assignment you are requested to implement insert, search, and delete operations for an open-addressing hash table with double hashing. Create an empty hash table of size m= 13. Each integer of the input will be a key that you should insert into the hash table. Use the double hashing function h{k, i) = (hı(k) + ih2(k)) mod 13 where hi(k)= k mod 13 and h2(k) = 1+(k mod 11). The...

  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public...

    IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public QuadraticProbingHashTable( int size ): As the signature of this method suggests, this is a constructor for this class. This constructor will initialize status of an object from this class based on the input parameter (i.e., size). So, this function will create the array HashTable with the specified size and initialize all of its elements to be null. 2. public int hash(int value, int tableSize...

  • The task of this project is to implement in Java Hash Table structure using Linear Probing...

    The task of this project is to implement in Java Hash Table structure using Linear Probing Collision Strategy.   You can assume that no duplicates or allowed and perform lazy deletion (similar to BST). Specification Create a generic class called HashTableLinearProbe <K,V>, where K is the key and V is the value. It should contain a private static class, HashEntry<K,V>. Use this class to create array to represent Hashtable:             HashEntry<K,V> hashtable[]; Implement all methods listed below and test each method...

  • Implement a software program in C++ t stores and searches the Student records using double-hashing algorithm.  Double...

    Implement a software program in C++ t stores and searches the Student records using double-hashing algorithm.  Double hashing uses the idea of applying a second hash function to key when a collision occurs.   The software program will be based on the following requirements: Development Environment: If the software program is written in C++, its project must be created using Microsoft Visual Studio 2017. If the software program is written in Java, its project must be created using NetBeans v8.2. Algorithm: If...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT