Question

In: Computer Science

Develop an algorithm to demonstrate hashing using hash table with modulo as the hash function. Assume...

Develop an algorithm to demonstrate hashing using hash table with modulo as the hash function. Assume the size of the hash table as 10. To avoid collisions in case of identical keys for two different elements, use Linear Probing collision resolution technique.
using c++
add comment on the code

Solutions

Expert Solution

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int SIZE = 10;//table size 10

//node for hash table for linear probing
class Node
{
public://attributes
int key;
int value;
Node(int key, int value)//constructor
{
this->key = key;
this->value = value;
}
};

//to delete node from table
class ClearNode:public Node
{
private:
static ClearNode *item;
ClearNode():Node(-1, -1)
{}
public:
static ClearNode *getNode()
{
if (item == NULL)
item = new ClearNode();
return item;
}
};
ClearNode *ClearNode::item = NULL;

class HashTable//class for hash table
{
private://attribute
Node **hashtable;
public:
HashTable()//cnstructor
{
hashtable = new Node* [SIZE];//creating hash table with size 10
for (int i = 0; i < SIZE; i++)
{
hashtable[i] = NULL;
}
}

~HashTable()//deleting table
{
for (int i = 0; i < SIZE; i++)
{
if (hashtable[i] != NULL && hashtable[i] != ClearNode::getNode())
delete hashtable[i];
}
delete[] hashtable;
}
//modulo hash function
int HashFunc(int key)
{
return key % SIZE;
}
//method to insert into hash table
void Insert(int key, int value)
{
int hash_val = HashFunc(key);
int init = -1;
int deletedindex = -1;
while (hash_val != init && (hashtable[hash_val]
== ClearNode::getNode() || hashtable[hash_val]
!= NULL && hashtable[hash_val]->key != key))
{
if (init == -1)
init = hash_val;
if (hashtable[hash_val] == ClearNode::getNode())
deletedindex = hash_val;
hash_val = HashFunc(hash_val + 1);
}
if (hashtable[hash_val] == NULL || hash_val == init)
{
if(deletedindex != -1)
hashtable[deletedindex] = new Node(key, value);
else
hashtable[hash_val] = new Node(key, value);
}
if(init != hash_val)
{
if (hashtable[hash_val] != ClearNode::getNode())
{
if (hashtable[hash_val] != NULL)
{
if (hashtable[hash_val]->key == key)
hashtable[hash_val]->value = value;
}
}
else
hashtable[hash_val] = new Node(key, value);
}
}
  
   //method to remove element from hash table   
void Remove(int key)
{
int hash_val = HashFunc(key);
int init = -1;
while (hash_val != init && (hashtable[hash_val]
== ClearNode::getNode() || hashtable[hash_val]
!= NULL && hashtable[hash_val]->key != key))
{
if (init == -1)
init = hash_val;
hash_val = HashFunc(hash_val + 1);
}
if (hash_val != init && hashtable[hash_val] != NULL)
{
delete hashtable[hash_val];
hashtable[hash_val] = ClearNode::getNode();
}
}
};

int main()
{
HashTable hash;
int key, value;
int choice;
while(1)
{
cout<<" Hash Table Using linear probing"<<endl;
cout<<"1.Insert"<<endl;
cout<<"2.Delete"<<endl;
cout<<"3.Stop"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter element: ";
cin>>value;
cout<<"Enter key: ";
cin>>key;
hash.Insert(key, value);
break;
case 2:
cout<<"Enter key: ";
cin>>key;
hash.Remove(key);
break;
case 3:
exit(1);
default:
cout<<"\nInvalid option\n";
}
}
return 0;
}

output:

Hash Table Using linear probing
1.Insert
2.Delete
3.Stop
Enter your choice: 1
Enter element: 1
Enter key: 2
Hash Table Using linear probing
1.Insert
2.Delete
3.Stop
Enter your choice: 2
Enter key: 2
Hash Table Using linear probing
1.Insert
2.Delete
3.Stop
Enter your choice: 3


Process exited with return value 1
Press any key to continue . . .



Related Solutions

3. Assume a hash table with 7 locations and the hashing function h(i) = i%7. Show...
3. Assume a hash table with 7 locations and the hashing function h(i) = i%7. Show the hash table that results when the integers are inserted in the order given. (10 points each. Total 30) • 5, 11, 18, 23, 28, 13, 25, with collisions resolved using linear probing • 5, 11, 18, 23, 28, 13, 25, with collisions resolved using quadratic probing • 5, 11, 18, 23, 28, 13, 25, with collisions resolved using chaining.
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
3. Draw the hash table that results using the hash function: h(k)=kmod 7 to hash the...
3. Draw the hash table that results using the hash function: h(k)=kmod 7 to hash the keys 50, 700, 76, 85, 92, 73, 101. Assuming collisions are handled by Quadratic probing. Don't write a program. Just please manually solve the problem. Thanks.
Data Structures: Hashing Exercise: 1. a) Given an empty “separate chaining hash table” of size 10,...
Data Structures: Hashing Exercise: 1. a) Given an empty “separate chaining hash table” of size 10, with hash function                        Location = Number modulus 10 Show the hash table after inserting the following numbers: 21, 18, 27 , 31 , 48, 51 , 37, 98, 17 b) What table size would be better? 11? 12? 20? 2. We want to hash name strings into a chaining hash table of size 10, how would you divide the alphabet into 10 groups?...
Write an algorithm to delete an element from a hash table that uses linear probing as...
Write an algorithm to delete an element from a hash table that uses linear probing as its clash resolution strategy. Analyze your algorithm and show the results using order notation?
Demonstrate Karatsuba’s algorithm for multiplying integers using just three half-size multiplications as detailed below. Assume that...
Demonstrate Karatsuba’s algorithm for multiplying integers using just three half-size multiplications as detailed below. Assume that you can use a calculator to do all operations (half-size (2 or 3 digit) multiplication, and adding, subtracting, and shifting of any size), but write down everything you do. (you are also encouraged to use a calculator to check your process by doing the 4-digit multiplication directly). Suppose we want to multiply 3275 · 2876. Write here the three half-size products that you are...
(10 pts) Draw the result of hashing 11, 22, 3, and 43 into a table using...
(10 pts) Draw the result of hashing 11, 22, 3, and 43 into a table using the following hash function, using separate chaining: h(x) = x % 4                0 1 2 3 (10 pts) Draw the result of hashing 11, 22, 3, and 43 into a table using the following hash function, using open addressing with linear probing: h(x) = x % 4 0 1 2 3
Develop a program using python to demonstrate data exchange using USB protocol.
Develop a program using python to demonstrate data exchange using USB protocol.
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page faults and page hits by considering the Frame size=4, ReferenceString:2 4 6 7 8 2 4 9 13 9 2 7 2 6 1 4 9 2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT