-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain_hash.cpp
More file actions
64 lines (60 loc) · 1.21 KB
/
chain_hash.cpp
File metadata and controls
64 lines (60 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
using namespace std;
int bucket =7;
int hashfunction(int x)
{
return(x%bucket);
}
void hashSearch(list<int> table[],int x)
{
int k=hashfunction(x);
list<int> :: iterator i;
for(i=table[k].begin();i!=table[k].end();i++)
{
if(*i==x)
{cout<<"Found"<<endl;
return;}
}
cout<<"NOT FOUND"<<endl;
if(i!=table[k].end())table[k].erase(i);
}
void hashDelete(list<int> table[],int x)
{
int k=hashfunction(x);
list<int> :: iterator i;
for(i=table[k].begin();i!=table[k].end();i++)
{
if(*i==x)break;
}
if(i!=table[k].end())table[k].erase(i);
}
void hashInsert(list <int> table[],int x)
{
int k=hashfunction(x);
table[k].push_back(x);
}
void hashdisplay(list <int> table[])
{
for(int i=0;i<bucket;i++)
{
cout<<i;
for (auto j : table[i])
{
cout<< " --> "<<j;
}
cout<<endl;
}
}
int main()
{
int arr[]={1,2, 3, 4 ,5 ,6 ,7 ,8 ,9 , 10 , 21,25 ,100};
int n=sizeof(arr)/sizeof(arr[0]);
list<int>table[bucket];
for(int i=0;i<n;i++)
{
hashInsert(table ,arr[i]);
}
hashDelete(table,100);
hashSearch(table,100);
hashSearch(table,9);
}