-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.cpp
178 lines (168 loc) · 2.08 KB
/
Hash.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include<iostream>
using namespace std;
class hashfun
{
private:
int *top;
int *temp;
int total;
int value;
int count;
int pre;
int *temp2;
public:
hashfun()
{
count=0;
cout<<"Enter How many items you want in a heap";
cin>>total;
pre=total;
top=new int[total];
for(int i=0;i<total;i++)
{
top[i]=-1;
}
}
void insert(int value)
{
int chk=total/2;
if(count==chk)
{
rehash();
return;
}
int i=value%total;
if(i==total-1)
{
if(top[i]!=-1)
{
i=0;
}
else if(top[i]==-1)
{
top[i]=value;
return;
}
}
while(i!=total-1)
{
cout<<i;
if(top[i]==-1)
{
count++;
top[i]=value;
return;
}
else if(top[i]!=-1)
{
i++;
}
}
}
void deletion()
{
}
void print()
{
cout<<endl<<"Here is"<<endl;
for(int i=0;i<total;i++)
{
cout<<top[i];
}
}
void search()
{
cout<<"Dear User!! Please Enter The Value to Search";
cin>>value;
int i=value%total;
if(count==0)
{
cout<<"Hash is Empty";
return;
}
if(i==total-1)
{
if(top[i]==value)
{
cout<<"Item Successfully Found";
return;
}
else
{
i=0;
}
}
while(i!=total-1)
{
if(top[i]==value)
{
cout<<"Value Found";
return;
}
else
{
i++;
}
}
}
void rehash()
{
int cc;
cout<<"Rehash Called";
total=total*2;
while(1)
{
if(primefinder(total)==true)
{
break;
}
else
total++;
}
temp=new int[total];
for(int i=0;i<total;i++)
{
temp[i]=-1;
}
temp2=top;
top=temp;
for(int i=0;i<pre;i++)
{
if(temp2[i]!=-1)
{
insert(temp2[i]);
}
}
delete temp2;
}
bool primefinder(int t)
{int count=0;
cout<<"Enter";
for(int a=1;a<=t;a++)
{
if(t%a==0)
{
count++;
}
}
if(count==2)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
int val;
hashfun h1;
for(int i=0;i<6;i++)
{
cin>>val;
h1.insert(val);
}
h1.print();
}