-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashimpl.java
More file actions
69 lines (55 loc) · 1.36 KB
/
Hashimpl.java
File metadata and controls
69 lines (55 loc) · 1.36 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
65
66
67
68
69
package IntrotoDSwithJava;
import java.util.Arrays;
public class Hashimpl {
int[] arr;
int capacity;
Hashimpl(){
arr = new int[20];
for(int i=0;i < 20;i++){
arr[i] = 0;
}
capacity = arr.length;
}
int hashfunc(int n){
return n%10;
}
boolean insert(int n){
int home = hashfunc(n);
int count = 0;
while( (arr[home] !=0) && home < capacity){
home++; count++;
if(home==capacity) home=0;
if(count == capacity) { System.out.println("Array is full");return false;}
}
arr[home] = n;
return true;
}
void printarr(){
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
Hashimpl impl = new Hashimpl();
impl.insert(1);
impl.insert(41);
impl.insert(51);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(61);
impl.insert(91);
impl.printarr();
}
}