Write a program to implement Symbol Table using the concept of Hashing with separate chaining. The idea is to make each cell of hash table point to a linked list of records that have same hash function value.
Input: The identifier, its type and line number , which may contain repetitive entries.
Hashing function : a) Compute the sum of ASCII values for a given input to identify the cell in hash table b) The size of the hash table is 100.
Expected output: i) The hash table (Symbol table) along with identifiers, type, line number
#include<bits/stdc++.h>
using namespace std;
class Hashing
{
int buc;
list<int> *tab;
public:
Hashing(int b)
{
buc = b;
tab = new list<int>[buc];
}
void insert(int k)
{
int i = hash_func(k);
tab[i].push_back(k);
}
void delete(int k)
{
int indx = hash_func(k);
list <int> :: iterator i;
for (i = tab[indx].begin();
i != tab[indx].end(); i++) {
if (*i == k)
break;
}
if (i != tab[indx].end())
tab[indx].erase(i);
}
int hash_func(int x) {
return (x % buc);
}
void display() {
for (int i = 0; i < buc; i++) {
cout << i;
for (auto x : tab[i])
cout << " : " << x;
cout << endl;
}
}
};
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int len = sizeof(arr)/sizeof(arr[0]);
Hashing hs(29);
for (int i = 0; i < len; i++)
hs.insert(arr[i]);
// hs.delete(4);
hs.display();
return 0;
}
Comments
Leave a comment