Answer to Question #335812 in C++ for Cumon

Question #335812

Note: Don't use any Built-in-library or Standard Library List. Solve without using any Library.

Please write class for hash table using chaining and it must contain the following  functions:
  • Please insert an element in table
  • Please search an element in table
  • Please delete an element in table
  • Please return no of the chains currently present
  • Please display the hash table of the class
  • Please write the main function to test the class created.

Note: Don't use any Built-in-library or Standard Library List. Solve without using any Library.

1
Expert's answer
2022-04-30T10:21:47-0400
#include<iostream>

using namespace std;


class Hash
{
	int BUCKET; // No. of buckets
				// Pointer to an array containing buckets
	int **table;
public:
	Hash(int V)
	{
		this->BUCKET = V;
		table = new int*[BUCKET];
		for (int i = 0; i < BUCKET; i++)
		{
			table[i] = new int[BUCKET];
			for(int j = 0; j < BUCKET; j++)
				table[i][j] = 0;
		}
	}
	// inserts a key into hash table
	void insertItem(int x)
	{
		int index = hashFunction(x);
		for (int i = 0; i < BUCKET; i++)
		{
			if (table[index][i] == 0)
			{
				table[index][i] = x;
				break;
			}
		}
	}
	// deletes a key from hash table
	void deleteItem(int key)
	{
		int index = hashFunction(key);
		// find the key in (index)th list
		for (int i = 0; i < BUCKET; i++)
		{
			if (table[index][i] == key)
			{
				table[index][i] = 0;
				break;
			}
		}
	}
	// search a key in hash table
	bool searchItem(int key)
	{
		int index = hashFunction(key);
		// find the key in (index)th list
		for (int i = 0; i < BUCKET; i++)
		{
			if (table[index][i] == key)
			{
				return true;
			}
		}
		return false;
	}
	int no_of_the_chains()
	{
		int res = 0;
		for (int i = 0; i < BUCKET; i++)
		{
			if (table[i][0]!=0)
				res++;
		}
		return res;
	}


	// hash function to map values to key
	int hashFunction(int x)
	{
		return (x % BUCKET);
	}
	// function to display hash table
	void displayHash()
	{
		for (int i = 0; i < BUCKET; i++)
		{
			cout << i;
			for (int j = 0; j < BUCKET; j++)
			{
				if (table[i][j] == 0)
					break;
				cout << " --> " << table[i][j];
			}
			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();
	cout << "h.searchItem(11)= " << h.searchItem(11) << endl;
	cout << "h.searchItem(12)= " << h.searchItem(12) << endl;
	cout << "h.no_of_the_chains()= " << h.no_of_the_chains() << endl;
	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