-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImplemnetation of Open Addressing.cpp
87 lines (80 loc) · 1.52 KB
/
Implemnetation of Open Addressing.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
using namespace std;
struct MyHash
{
int *arr;
int cap,size;
MyHash(int c)
{
cap=c;
size=0;
arr=new int[cap];
for(int i=0;i<cap;i++)
arr[i]=-1;
}
int hash(int key){
return key%cap;
}
bool insert(int key)
{
if(size==cap)
return false;
int i=hash(key);
while(arr[i]!=-1 && arr[i]!=-2 && arr[i]!=key)
i=(i+1)%cap;
if(arr[i]==key)
return false;
else{
arr[i]=key;
size++;
return true;
}
}
bool search(int key)
{
int h=hash(key);
int i=h;
while(arr[i]!=-1)
{
if(arr[i]==key)
return true;
i=(i+1)%cap;
if(i==h)
return false;
}
return false;
}
bool erase(int key)
{
int h=hash(key);
int i=h;
while(arr[i]!=-1)
{
if(arr[i]==key)
{
arr[i]=-2;//mark for a deleted element
return true;
}
i=(i+1)%cap;
if(i==h)
return false;
}
return false;
}
};
int main()
{
MyHash mh(7);
mh.insert(49);
mh.insert(56);
mh.insert(72);
if(mh.search(56)==true)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
mh.erase(56);
if(mh.search(56)==true)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}