hashtable linear probing ,insertion deletion searching
//all the basic opertaion insertion , deletion ,searching,displaying
#include <iostream>
using namespace std;
void init(int arr[],int size)
{
for(int i=0;i<size;i++)
{
arr[i]=-1;
}
}
void inserthash(int arr[],int size,int value)
{
int key=value%size;
int index=key;
while(arr[index]!=-1)
{
index=(index+1)%size;
if(index==key)
{
cout<<"hash table full"<<endl;
}
}
arr[index]=value;
}
void deletehash(int arr[],int size,int value)
{
int key=value%size;
int index=key;
while(arr[index]!=value)
{
index=(index+1)%size;
if(index==key)
{
cout<<"value to be deleted not found"<<endl;
}
}
arr[index]=-1;
}
int searchhash(int arr[],int size,int value)
{
int key=value%size;
int index=key;
while(arr[index]!=value)
{
index=(index+1)%size;
if(index==key)
{
return 0;
}
}
return 1;
}
void display(int arr[],int size)
{
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main()
{
int n;
cout<<"enter the size:"<<endl;
cin>>n;
int a[n];
init(a,n);
int choice;
while(1)
{
cout<<"1. insert"<<endl<<"2.delete"<<endl<<"3.search"<<endl<<"4. display"<<endl<<"5. exit"<<endl;
cout<<"enter the choice:"<<endl;
cin>>choice;
switch(choice)
{
case 1:
{
int val;
cout<<"enter the value you want to insert:"<<endl;
cin>>val;
inserthash(a,n,val);
break;
}
case 2:
{
int val;
cout<<"enter the value you want to delete:"<<endl;
cin>>val;
deletehash(a,n,val);
break;
}
case 3:
{
int val;
cout<<"enter the value to be searched:"<<endl;
cin>>val;
int l= searchhash(a,n,val);
if(l==1)
{
cout<<"value found"<<endl;
}
else
{
cout<<"not found"<<endl;
}
break;
}
case 4:
{
display(a,n);
break;
}
case 5:
{
exit(0);
}
default:
{
cout<<"invalid choice"<<endl;
}
}
}
}
Are there any code examples left?
New code examples in category C++
-
C++ 2023-04-28 17:44:25
-
C++ 2022-03-27 19:20:39 lists occurrences of characters in the string c++
-
C++ 2022-03-27 18:00:14 variabili in c++
-
C++ 2022-03-27 17:10:08 repeat character n times c++
-
C++ 2022-03-27 15:50:07 delete an array c++
-
C++ 2022-03-27 15:40:12 C++ pointer to base class
-
C++ 2022-03-27 12:15:21 find the graph is minimal spanig tree or not
-
C++ 2022-03-27 11:30:15 multi variable assignment cpp
-
C++ 2022-03-27 11:05:17 c++ pi float
-
C++ 2022-03-27 10:20:12 why exceptions can lead to memory leaks