Answer to Question #265647 in C++ for Asjad

Question #265647

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


1
Expert's answer
2021-11-14T10:43:13-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS