Question

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 program, you will find the referenced, and incomplete program: "hashing with chaining using singly linked lists.

// 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;
}

---Below is the referenced "hashing with chaining using doubly linked list" program. As stated in the instructions above, please write the remove function for this program and test it with the rest of the program including the given main program

#include <iostream>

using namespace std;

  const int tablesize = 25;

  

// declaration of node

struct hash_node {

    int val, key;

    hash_node* next;

    hash_node* prev;

};

  

// hashmap's declaration

class HashMap {

public:

    hash_node **hashtable, **top;

  

    // constructor

    HashMap()

    {

        // create a empty hashtable

        hashtable = new hash_node*[tablesize];

        top = new hash_node*[tablesize];

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

            hashtable[i] = NULL;

            top[i] = NULL;

        }

    }

  

    // destructor

    ~HashMap()

    {

        delete[] hashtable;

    }

  

    // hash function definition

    int HashFunc(int key)

    {

        return key % tablesize;

    }

  

    // searching method

    void find(int key)

    {

        // Applying hashFunc to find

        // index for given key

        int hash_val = HashFunc(key);

        bool flag = false;

        hash_node* entry = hashtable[hash_val];

  

        // if hashtable at that index has some

        // values stored

        if (entry != NULL) {

            while (entry != NULL) {

                if (entry->key == key) {

                    flag = true;

                }

                if (flag) {

                    cout << "Element found at key "

                         << key << ": ";

                    cout << entry->val << endl;

                }

                entry = entry->next;

            }

        }

        if (!flag)

            cout << "No Element found at key "

                 << key << endl;

    }

  

//Removing an element to be done by the student.

  

    // inserting method

    void add(int key, int value)

    {

        // Applying hashFunc to find

        // index for given key

        int hash_val = HashFunc(key);

        hash_node* entry = hashtable[hash_val];

  

        // if key has no value stored

        if (entry == NULL) {

            // creating new node

            entry = new hash_node;

            entry->val = value;

            entry->key = key;

            entry->next = NULL;

            entry->prev = NULL;

            hashtable[hash_val] = entry;

            top[hash_val] = entry;

        }

  

        // if some values are present

        else {

            // traversing till the end of

            // the list

            while (entry != NULL)

                entry = entry->next;

  

            // creating the new node

            entry = new hash_node;

            entry->val = value;

            entry->key = key;

            entry->next = NULL;

            entry->prev = top[hash_val];

            top[hash_val]->next = entry;

            top[hash_val] = entry;

        }

        cout << "Value " << value << " was successfully"

                " added at key " << key << endl;

    }

};

  

// Driver Code

int main()

{

    HashMap hash;

    hash.add(4, 5);

    hash.find(4);

    hash.remove(4);

    return 0;

}

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

Here is the required code for the problem

#include <iostream>

using namespace std;

const int tablesize = 25;

  

// declaration of node

struct hash_node {

int val, key;

hash_node* next;

hash_node* prev;

};

  

// hashmap's declaration

class HashMap {

public:

hash_node **hashtable, **top;

  

// constructor

HashMap()

{

// create a empty hashtable

hashtable = new hash_node*[tablesize];

top = new hash_node*[tablesize];

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

hashtable[i] = NULL;

top[i] = NULL;

}

}

  

// destructor

~HashMap()

{

delete[] hashtable;

}

  

// hash function definition

int HashFunc(int key)

{

return key % tablesize;

}

  

// searching method

void find(int key)

{

// Applying hashFunc to find

// index for given key

int hash_val = HashFunc(key);

bool flag = false;

hash_node* entry = hashtable[hash_val];

  

// if hashtable at that index has some

// values stored

if (entry != NULL) {

while (entry != NULL) {

if (entry->key == key) {

flag = true;

}

if (flag) {

cout << "Element found at key "

<< key << ": ";

cout << entry->val << endl;

}

entry = entry->next;

}

}

if (!flag)

cout << "No Element found at key "

<< key << endl;

}

  

//Removing an element to be done by the student.
void remove(int key)
{
// Applying hashFunc to find

// index for given key

int hash_val = HashFunc(key);

hash_node* entry = hashtable[hash_val];

if (entry->key != key || entry == NULL) {

cout << "no element at this key"

<< key << endl;

return;

}
  
// if any value is present at that key &

// traversing the list and removing all values

while (entry != NULL) {

if (entry->next == NULL) {

if (entry->prev == NULL) {

hashtable[hash_val] = NULL;

top[hash_val] = NULL;

delete entry;

break;

}
else
{
top[hash_val] = entry->prev;

top[hash_val]->next = NULL;

delete entry;

entry = top[hash_val];
}

}

entry = entry->next;

}
cout << "Element was successfully removed at the key "
<< key << endl;
}

  

// inserting method

void add(int key, int value)

{

// Applying hashFunc to find

// index for given key

int hash_val = HashFunc(key);

hash_node* entry = hashtable[hash_val];

  

// if key has no value stored

if (entry == NULL) {

// creating new node

entry = new hash_node;

entry->val = value;

entry->key = key;

entry->next = NULL;

entry->prev = NULL;

hashtable[hash_val] = entry;

top[hash_val] = entry;

}

  

// if some values are present

else {

// traversing till the end of

// the list

while (entry != NULL)

entry = entry->next;

  

// creating the new node

entry = new hash_node;

entry->val = value;

entry->key = key;

entry->next = NULL;

entry->prev = top[hash_val];

top[hash_val]->next = entry;

top[hash_val] = entry;

}

cout << "Value " << value << " was successfully"

" added at key " << key << endl;

}

};

  

// Driver Code

int main()

{

HashMap hash;

hash.add(4, 5);

hash.find(4);

hash.remove(4);

return 0;

}

here is the ss of the output

Value 5 was successfully added at key 4 Element woundsuctesstuiiy removed at the key 4 Element was successfully removed at t

I hope this will help you so please give positive ratings :)))

Add a comment
Know the answer?
Add Answer to:
Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...
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
  • 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)...

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

  • Identify the letters and anything associated with it. It is a hash map so please feel...

    Identify the letters and anything associated with it. It is a hash map so please feel free to point out the other important parts beside the arrows. I apologize for the order of the pictures class HashMap private: HashEntry table ; public: HashMap() table-new HashEntry * [TABLE_SIZE]; for (int 1-0; 1< TABLE SIZE: i++) table [i] = NULL; Hash Function int HashFunc(int key) return key % TABLE-SIZE; Insert Element at a key void Insert(int key, int value) int hashHashFunc(key); while...

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

  • Hashtable in C Please fix void ht_put(hashtable_t *ht, char *key, void *val) and void free_hashtable(hashtable_t *ht)...

    Hashtable in C Please fix void ht_put(hashtable_t *ht, char *key, void *val) and void free_hashtable(hashtable_t *ht) Implement void ht_del(hashtable_t *ht, char *key) and void ht_rehash(hashtable_t *ht, unsigned long newsize) --------------[hashtable.h]--------------------------------------- -######################################### #ifndef HASHTABLE_T #define HASHTABLE_T typedef struct hashtable hashtable_t; typedef struct bucket bucket_t; struct bucket { char *key; void *val; bucket_t *next; }; struct hashtable { unsigned long size; bucket_t **buckets; }; unsigned long hash(char *str); hashtable_t *make_hashtable(unsigned long size); void ht_put(hashtable_t *ht, char *key, void *val); void *ht_get(hashtable_t *ht,...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

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

  • In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits&gt...

    In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...

  • Type up and get the Hash table on pages 19 - 22 to work. Show your...

    Type up and get the Hash table on pages 19 - 22 to work. Show your sample test run at the bottom of your code using a multiline comment I typed everything but the code is not working please help me fix the mistakes. These are pages 19-22. The code I have: #include <iostream> #inlcude <iomanip> #include <stack> #include <vector> #include <cstdlib> #include <ctime> using namespace std; //////////////////////////////HASH TABLE/////////////////////////////////////////////// //hash.cpp //demonstrate hash table with linear probing /////////////////////////////////////////////////////////////////////////////////////// class DataItem {...

  • Bucket Management – In this programming exercise you will create a simple bucket management program according...

    Bucket Management – In this programming exercise you will create a simple bucket management program according to the following customer specifications: Write the definition of a class Bucket, to implement the properties and functions of a bucket. The bucket must be Cylinder shaped of any dimension. (20%) The class should have the instant variables to store the height (in feet), radius (in feet), amount (in cubic feet) of water in the bucket, the rate (in gallons / minute), at which...

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