November 21, 2014

C++ Program to Implement Static Hashing

static hashing, how to implement a hash table, indexing and hashing in data structure, dictionary implementation data structure

 C++ Program to Implement Static Hashing

 #include <iostream>
 using namespace std; 
int table_size=10; 
void main() 
 { 
 int a[10],n,h=0,count=0,flag=0,element; 
 //Inserting
 for(int i=0;i<10;i++) 
 a[i]=0; 
 cout<<"\nEnter the elements: \n"; 
 while(count<table_size) 
 { 
 cin>>n; 
 h=n%10; 
 if(h>=table_size) 
 h=0; 
 if(a[h]==0) 
 a[h]=n; 
 else //incase of overflow
 { 
 while(flag==0) 
 { 
 if(h>=table_size) 
 h=0; 
 if(a[h]==0) 
 { 
 a[h]=n; 
 flag=1; 
 } 
 else
 h++; 
 } 
 flag=0; 
 } 
 count++; 
 }//end while
 //displaying the array
 cout<<"The elements are: \n"; 
 for(int j=0;j<table_size;j++) 
 cout<<a[j]<<"\t"; 
 cout<<"\n\n"; 
 h=0,flag=0; 
 //To search for an element cout<<"\nEnter the element you want to search\n"; 
 cin>>element; 
 for(int k=0;k<10;k++) 
 { 
 if(a[k]==element) 
 { 
 flag=1; 
 h=k; 
 } 
 } 
 if(flag==1) 
 cout<<"\nElement "<<element<<" found at key 
"<<h<<"!!!\n"; 
 else
 cout<<"\nElement not found\n"; 
 system("pause"); 
 }//end main 

C++ Program

C++ Program

No comments:
Write comments