Repository navigation
Expand file tree
/
Copy pathHash_table.java
More file actions
107 lines (89 loc) · 1.78 KB
/
Copy pathHash_table.java
File metadata and controls
107 lines (89 loc) · 1.78 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
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
public class Hash_table {
int HashSize = 100;
Hash[] h;
public int hashFunction(int element) {
int key;
key = element;
key += ~(key << 16);
key ^= (key >> 5);
key += (key << 3);
key ^= (key >> 13);
key += ~(key << 9);
key ^= (key >> 17);
return key % HashSize;
}
Hash_table() {
h = new Hash[HashSize];
for (int i = 0; i < HashSize; i++) {
h[i] = new Hash();
h[i].key = i;
}
}
public void hashSearch(int element) {
int value = hashFunction(element);
List l = new List();
int i = 0;
while (value != h[i].key) {
i++;
}
if (h[i].head == null) {
System.out.println("No such element!");
return;
}
l = h[i].head;
while (l.val != element) {
if (l.next == null) {
System.out.println("No such element!");
return;
}
l = l.next;
}
System.out.println("Element was found!");
}
public void hashAdd(int element) {
int value = hashFunction(element);
List l = new List();
int i = 0;
while (value != h[i].key) {
i++;
}
l = h[i].head;
if (l == null) {
List new_list = new List();
h[i].head = new_list;
h[i].head.val = element;
h[i].head.next = null;
} else {
while (l.next != null) {
l = l.next;
}
List new_data = new List();
new_data.val = element;
l.next = new_data;
}
}
public void hashDelete(int element) {
int value = hashFunction(element);
List l = new List();
int i = 0;
while (value != h[i].key) {
i++;
}
if (h[i].head == null) {
System.out.println("No such element!");
return;
}
l = h[i].head;
while (l.next != null) {
if (l.next.val == element) {
l.next = l.next.next;
System.out.println("Element was successfully deleted!");
return;
} else {
l = l.next;
}
}
System.out.println("No such element!");
return;
}
}