-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.java
More file actions
61 lines (61 loc) · 1.7 KB
/
Copy pathHashTable.java
File metadata and controls
61 lines (61 loc) · 1.7 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
public class HashTable {
private final Node[] table;
private final int size;
public HashTable(int size) {
this.size = size;
this.table = new Node[size];
}
private int hashFunc(Square square) {
int key = (int)square.P;
return key % this.size;
}
public boolean insert(Square square) {
int index = hashFunc(square);
if (table[index] == null) {
table[index] = new Node(square);
return true;
}
else{
Node cur = table[index];
while (cur.next != null) {
if (cur.data.equals(square)) {
return false;
}
cur = cur.next;
}
cur.next = new Node(square);
return true;
}
}
public void deleteArea(double area) {
for (int i = 0; i < size; i++) {
Node cur = table[i];
Node prev = null;
while (cur != null) {
if (cur.data.getArea() < area) {
if (prev == null) {
table[i] = cur.next;
}
else {
prev.next = cur.next;
}
}
else {
prev = cur;
}
cur = cur.next;
}
}
}
public void printTable() {
for (int i = 0; i < table.length; i++) {
if (table[i] != null) {
Node cur = table[i];
while (cur != null) {
System.out.println(i + " " + cur.data.toString());
cur = cur.next;
}
}
}
}
}