Question

Data Structure in C++

PROGRAMMING PROJECTS Implement the class umapHash Key, which creates a hash function for miniPair ob- iects that uses only th

please type the code and the output.
thanks

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

C++ program for hashing with chaining

In hashing there is a hash function that maps keys to some values. But these hashing function may lead to collision that is two or more keys are mapped to same value. Chain hashing avoids collision. The idea is to make each cell of hash table point to a linked list of records that have same hash function value.

Let’s create a hash function, such that our hash table has ‘N’ number of buckets.
To insert a node into the hash table, we need to find the hash index for the given key. And it could be calculated using the hash function.
Example: hashIndex = key % noOfBuckets

Insert: Move to the bucket corresponds to the above calculated hash index and insert the new node at the end of the list.

Delete: To delete a node from hash table, calculate the hash index for the key, move to the bucket corresponds to the calculated hash index, search the list in the current bucket to find and remove the node with the given key (if found).
Lets say hash table with 7 buckets (0, 1, 2, 3, 4, 5, 6) Keys arrive in the Order (15, 11, 27, 8) 2 3 4 5 627

Please refer Hashing | Set 2 (Separate Chaining) for details.

We use a list in C++ which is internally implemented as linked list (Faster insertion and deletion).

filter_none

edit

play_arrow

brightness_4

// CPP program to implement hashing with chaining

#include<iostream>

#include <list>

using namespace std;

  

class Hash

{

    int BUCKET;    // No. of buckets

  

    // Pointer to an array containing buckets

    list<int> *table;

public:

    Hash(int V); // Constructor

  

    // inserts a key into hash table

    void insertItem(int x);

  

    // deletes a key from hash table

    void deleteItem(int key);

  

    // hash function to map values to key

    int hashFunction(int x) {

        return (x % BUCKET);

    }

  

    void displayHash();

};

  

Hash::Hash(int b)

{

    this->BUCKET = b;

    table = new list<int>[BUCKET];

}

  

void Hash::insertItem(int key)

{

    int index = hashFunction(key);

    table[index].push_back(key);

}

  

void Hash::deleteItem(int key)

{

  // get the hash index of key

  int index = hashFunction(key);

  

  // find the key in (inex)th list

  list <int> :: iterator i;

  for (i = table[index].begin();

           i != table[index].end(); i++) {

    if (*i == key)

      break;

  }

  

  // if key is found in hash table, remove it

  if (i != table[index].end())

    table[index].erase(i);

}

  

// function to display hash table

void Hash::displayHash() {

  for (int i = 0; i < BUCKET; i++) {

    cout << i;

    for (auto x : table[i])

      cout << " --> " << x;

    cout << endl;

  }

}

  

// Driver program

int main()

{

  // array that contains keys to be mapped

  int a[] = {15, 11, 27, 8, 12};

  int n = sizeof(a)/sizeof(a[0]);

  

  // insert the keys into the hash table

  Hash h(7);   // 7 is count of buckets in

               // hash table

  for (int i = 0; i < n; i++)

    h.insertItem(a[i]);  

  

  // delete 12 from hash table

  h.deleteItem(12);

  

  // display the Hash table

  h.displayHash();

  

  return 0;

}

Add a comment
Know the answer?
Add Answer to:
PROGRAMMING PROJECTS Implement the class umapHash Key, which creates a hash function for miniPair...
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
  • problem, you will implement a no virtual base class template using the Curiously Recurring Template Pattern...

    problem, you will implement a no virtual base class template using the Curiously Recurring Template Pattern n this or CRTP Your task is to define the Comparable template, implementing the following functions in terms of the Derived class operator<: operator operator operator operator operator Additionally, each function should be noexcept and const-qualified. Please use C++. And also I want a answer implementint the functions in term of the Derived class operator<. There is a question on this website before but...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • C++ 22.5* (Remove elements) Implement the following function that removes the specified value from a first-class...

    C++ 22.5* (Remove elements) Implement the following function that removes the specified value from a first-class container. Only the first occurrence of a matching value in the container is removed. template<typename ElementType, typename ContainerType> void remove(ContainerType& container, const ElementType& value) Your sample case should push the integer 1 through 6 onto a vector<int>, display the contents of the vector, remote the integer 4 from the vector, display the vector again, and attempt to remove the integer 4 a second time,...

  • Following class is only part of my program that uses a hash table to simulate a...

    Following class is only part of my program that uses a hash table to simulate a market's client database, client class is my main class which asks user to input client names and then creates a hash table which then users can look up client names. wasn't able to upload everything as it is too much code, but what I need is to modify my client class instead of me inputting data line by line, the program should read from...

  • Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the...

    Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This class should have two private data members, m_city and m_state, that are strings for storing the city name and state abbreviation for some Address b. Define and implement public getter and setter member functions for each of the two data members above. Important: since these member functions deal with objects in this case strings), ensure that your setters...

  • In a function template, a generic data type starts with the key word _____ followed by...

    In a function template, a generic data type starts with the key word _____ followed by a parameter name that stands for the data type. template class T function Which of the following blocks is designed to catch any type of exception? catch(){ } catch(...){ } catch(*){ } catch(exception){ } A friend function can only be a regular stand-alone function and can not be a member of another class. True False A function template is an actual function that the...

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • IN C++ Create a class to act as a generic array (i.e. the user will be...

    IN C++ Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on...

  • Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...

    Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you. // hash.CPP program to implement hashing with chaining #include<iostream> #include "hash.hpp" using namespace std; node* HashTable::createNode(int key, node* next) { node* nw = new node; nw->key = key; nw->next = next; return nw; } HashTable::HashTable(int bsize) { this->tableSize= bsize; table = new node*[tableSize]; for(int i=0;i<bsize;i++) table[i] = nullptr; } //function to calculate hash function unsigned int HashTable::hashFunction(int key)...

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