AIM: Write a program that implements Hash table using Linear Probing.
SOURCE CODE:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Hash{
public:
int *a,sz,count;
Hash(int n){
a=new int[sz=n];
for(int i=0;i<sz;i++){
a[i]=-1;
}
count=0;
}
void insert(int);
int delete1(int);
int search(int);
void display();
};
void Hash::insert(int x){
if(count==sz){
cout<<"\nHash Table Full";
}
else{
int key=x%sz;
int t=key;
if(a[key]==-1){
a[key]=x;
count++;
cout<<"\n"<<x<<" Element Inserted at "<<key;
}
else{
xyz:
if(key==sz-1)
key=-1;
key=key+1;
if(key==t){
return;
}
else{
if(a[key]==-1){
a[key]=x;
count++;
cout<<"\n"<<x<<" Element Inserted at "<<key;
return;
}
}
goto xyz;
}
}
}
int Hash::delete1(int ele){
int i;
int key=ele%sz;
if(count==0){
return -1;
}
else if(a[key]==ele){
a[key]=-1;
count--;
return key;
}
else{
for(i=0;i<sz;i++){
if(a[i]==ele){
a[i]==-1;
count--;
return i;
}
}
if(i==sz){
return INT_MAX;
}
}
}
int Hash::search(int ele){
int i;
int key=ele%sz;
if(count==0){
return -1;
}
else if(a[key]==ele){
return key;
}
else{
for(i=0;i<sz;i++){
if(a[i]==ele){
return i;
}
}
if(i==sz){
return INT_MAX;
}
}
}
void Hash::display(){
cout<<"Hash table is::"<<endl;
for(int i=0;i<sz;i++){
if(a[i]!=-1){
cout<<endl<<i<<" --> "<<a[i];
}
}
cout<<endl;
}
int main(){
int n,ele,ch,flag;
cout<<"\nEnter size of hash table::";
cin>>n;
Hash ob(n);
while(1){
cout<<"\nHash table operations are:";
cout<<"\n1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit\nEnter your choice::";
cin>>ch;
switch(ch){
case 1:
cout<<"\nEnter an element to insert::";
cin>>ele;
ob.insert(ele);
break;
case 2:
cout<<"\nEnter an element to delete::";
cin>>ele;
flag=ob.delete1(ele);
if(flag==INT_MAX){
cout<<"\nElement not in the hash table.";
}
if(flag==-1){
cout<<"\nHash table is empty.";
}
else{
cout<<"\nDeleted element "<<ele<<" at position "<<flag<<endl;
}
break;
case 3:
cout<<"\nEnter an element to search::";
cin>>ele;
flag=ob.search(ele);
if(flag==INT_MAX){
cout<<"\nElement not in the List\n";
}
else if(flag==-1){
cout<<"\nHash table is empty.";
}
else{
cout<<"\n"<<ele<<" element searched at position "<<flag<<endl;
}
break;
case 4:
ob.display();
break;
case 5:
exit(0);
default:
cout<<"\nWrong Choice\n";
}
}
return 0;
}
SOURCE CODE:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Hash{
public:
int *a,sz,count;
Hash(int n){
a=new int[sz=n];
for(int i=0;i<sz;i++){
a[i]=-1;
}
count=0;
}
void insert(int);
int delete1(int);
int search(int);
void display();
};
void Hash::insert(int x){
if(count==sz){
cout<<"\nHash Table Full";
}
else{
int key=x%sz;
int t=key;
if(a[key]==-1){
a[key]=x;
count++;
cout<<"\n"<<x<<" Element Inserted at "<<key;
}
else{
xyz:
if(key==sz-1)
key=-1;
key=key+1;
if(key==t){
return;
}
else{
if(a[key]==-1){
a[key]=x;
count++;
cout<<"\n"<<x<<" Element Inserted at "<<key;
return;
}
}
goto xyz;
}
}
}
int Hash::delete1(int ele){
int i;
int key=ele%sz;
if(count==0){
return -1;
}
else if(a[key]==ele){
a[key]=-1;
count--;
return key;
}
else{
for(i=0;i<sz;i++){
if(a[i]==ele){
a[i]==-1;
count--;
return i;
}
}
if(i==sz){
return INT_MAX;
}
}
}
int Hash::search(int ele){
int i;
int key=ele%sz;
if(count==0){
return -1;
}
else if(a[key]==ele){
return key;
}
else{
for(i=0;i<sz;i++){
if(a[i]==ele){
return i;
}
}
if(i==sz){
return INT_MAX;
}
}
}
void Hash::display(){
cout<<"Hash table is::"<<endl;
for(int i=0;i<sz;i++){
if(a[i]!=-1){
cout<<endl<<i<<" --> "<<a[i];
}
}
cout<<endl;
}
int main(){
int n,ele,ch,flag;
cout<<"\nEnter size of hash table::";
cin>>n;
Hash ob(n);
while(1){
cout<<"\nHash table operations are:";
cout<<"\n1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit\nEnter your choice::";
cin>>ch;
switch(ch){
case 1:
cout<<"\nEnter an element to insert::";
cin>>ele;
ob.insert(ele);
break;
case 2:
cout<<"\nEnter an element to delete::";
cin>>ele;
flag=ob.delete1(ele);
if(flag==INT_MAX){
cout<<"\nElement not in the hash table.";
}
if(flag==-1){
cout<<"\nHash table is empty.";
}
else{
cout<<"\nDeleted element "<<ele<<" at position "<<flag<<endl;
}
break;
case 3:
cout<<"\nEnter an element to search::";
cin>>ele;
flag=ob.search(ele);
if(flag==INT_MAX){
cout<<"\nElement not in the List\n";
}
else if(flag==-1){
cout<<"\nHash table is empty.";
}
else{
cout<<"\n"<<ele<<" element searched at position "<<flag<<endl;
}
break;
case 4:
ob.display();
break;
case 5:
exit(0);
default:
cout<<"\nWrong Choice\n";
}
}
return 0;
}
OUTPUT:
Enter size of hash table::3
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::1
Enter an element to insert::46
46 Element Inserted at 1
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::1
Enter an element to insert::74
74 Element Inserted at 2
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::1
Enter an element to insert::30
30 Element Inserted at 0
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::4
Hash table is::
0 --> 30
1 --> 46
2 --> 74
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::3
Enter an element to search::46
46 element searched at position 1
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::3
Enter an element to search::22
Element not in the List
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::2
Enter an element to delete::46
Deleted element 46 at position 1
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::4
Hash table is::
0 --> 30
2 --> 74
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::5
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::1
Enter an element to insert::46
46 Element Inserted at 1
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::1
Enter an element to insert::74
74 Element Inserted at 2
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::1
Enter an element to insert::30
30 Element Inserted at 0
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::4
Hash table is::
0 --> 30
1 --> 46
2 --> 74
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::3
Enter an element to search::46
46 element searched at position 1
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::3
Enter an element to search::22
Element not in the List
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::2
Enter an element to delete::46
Deleted element 46 at position 1
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::4
Hash table is::
0 --> 30
2 --> 74
Hash table operations are:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice::5