Question

This week you worked with hash tables and a variety of ways to handle collisions. For...

This week you worked with hash tables and a variety of ways to handle collisions. For this lab, please implement a hash table that uses a linked list to handle chaining for collision avoidance. You can be creative with your implementation, what I will grade on this lab is your effective hash table implementation ( linked list and chaining ).

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

Following is the required code:


#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
using namespace std;
class node
{
public:
int key;
int value;
node* next;
node(int key, int value)
{
this->key = key;
this->value = value;
this->next = NULL;
}
};
class map
{
private:
node** hash_table;
public:
map()
{
hash_table = new node*[100];
for (int i = 0; i < 100; i++)
hash_table[i] = NULL;
}
~map()
{
for (int i = 0; i < 100; ++i)
{
node* add_e = hash_table[i];
while (add_e != NULL)
{
node* previous = add_e;
add_e = add_e->next;
delete previous;
}
}
delete[] hash_table;
}
int hash_function(int key)
{
return key % 100;
}
void Insert(int key, int value)
{
int hash_value_e = hash_function(key);
node* previous = NULL;
node* add_e = hash_table[hash_value_e];
while (add_e != NULL)
{
previous = add_e;
add_e = add_e->next;
}
if (add_e == NULL)
{
add_e = new node(key, value);
if (previous == NULL)
{
hash_table[hash_value_e] = add_e;
}
else
{
previous->next = add_e;
}
}
else
{
add_e->value = value;
}
}
int search_e(int key)
{
bool flag = false;
int hash_value_e = hash_function(key);
node* add_e = hash_table[hash_value_e];
while (add_e != NULL)
{
if (add_e->key == key)
{
cout<<add_e->value<<" ";
flag = true;
}
add_e = add_e->next;
}
if (!flag)
return -1;
}
};
int main()
{
map hash;
int key, value;
int ch;
while (1)
{
cout<<"\n1.Insert element into the table"<<endl;
cout<<"2.search_e element from the key"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter element to be inserted: ";
cin>>value;
cout<<"Enter key at which element to be inserted: ";
cin>>key;
hash.Insert(key, value);
break;
case 2:
cout<<"\nEnter key of the element to be searched: ";
cin>>key;
cout<<"Element at key "<<key<<" : ";
if (hash.search_e(key) == -1)
{
cout<<"No element found at key "<<key<<endl;
continue;
}
break;
case 3:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return 0;
}

The output is shown below:

Add a comment
Know the answer?
Add Answer to:
This week you worked with hash tables and a variety of ways to handle collisions. For...
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
  • LINUX C programming Please make sure the output is correct Please do read data from file,...

    LINUX C programming Please make sure the output is correct Please do read data from file, the example below is just a example Third: Hash table (20 points) In this part, you will implement a hash table containing integers. The hash table has 10,000 buckets. An important part of a hash table is collision resolution. In this assignment, we want you to use chaining with a linked list to handle a collision. This means that if there is a collision...

  • 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...

  • PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and...

    PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and use the multiplication method to generate hash keys. You may use your own linked list code from a previous assignment or you can use a standard sequence container. Partial definitions for the hash table class and a generic data class are provided (C++ and Java versions). You may use them as a starting point if you wish. If you choose to design your own...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

  • Overview: The goal of this assignment is to implement a simple spell checker using a hash...

    Overview: The goal of this assignment is to implement a simple spell checker using a hash table. You will be given the basic guidelines for your implementation, but other than that you are free to determine and implement the exact classes and methods that you might need. Your spell-checker will be reading from two input files. The first file is a dictionary containing one word per line. The program should read the dictionary and insert the words into a hash...

  • Could someone please summarize the following for my programming class? They are study questions for java...

    Could someone please summarize the following for my programming class? They are study questions for java What an association list is. How to test if an association list is empty. How to find the value associated with a key in an association list. How to add a key-value pair to an association list. How to delete a key-value pair from an association list. How efficient an association list is (using O notation). What a circular list is. What a circular...

  • Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into...

    Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into hash table we insert at an index calculated by the key modulo the array size, what would happen if we instead did key mod (array_size*2), or key mod (array_size/2)? (Describe both cases). Theory answer Here Change your hashtable from the labs/assignments to be an array of linkedlists – so now insertion is done into a linkedlist at that index. Implement insertion and search. This...

  • Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...

    Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...

  • 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 this project, you will work on the algorithm (discussed in Module 1) to determine the...

    In this project, you will work on the algorithm (discussed in Module 1) to determine the length of the longest sub sequence of consecutive integers in an array You will implement the algorithm using Hash tables. You are provided with sample code (in C++ and Java) representing the linked list-based implementation of Hash tables (as an array of Linked Lists). You could go through the code to understand the implementation of a Hash table and the functions that can be...

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