In UG Boys Hostel Having 5 floors and each floor having 15 rooms. Consider you have to allocate the hostel room for students of IT department using hashing techniques (Students RollNo as Key). For only final year, one student per room and all other rooms are shareable at the maximum of three students per room. You may to use any strategy but must implement the separate chaining and Linear probing techniques in your solution.
#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