-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeznam.java
75 lines (62 loc) · 1.67 KB
/
Seznam.java
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
public class Seznam {
private Elt head;
private Seznam tail;
private static int comparisons = 0;
public Seznam (Elt elt, Seznam rep) {
this.head = elt;
this.tail = rep;
}
private static Seznam changeValue(Seznam s, Elt e) {
if (s == null) {
return new Seznam(e, null);
}
if (e.key == s.head.key){
return new Seznam(e, s.tail);
} else {
return new Seznam(s.head, changeValue(s.tail, e));
}
}
public static Seznam insert(Seznam s, Elt e) {
if (s == null) {
return new Seznam(e, null);
} else {
if (find(s, e.key) != null && find(s, e.key).key == e.key) {
return changeValue(s, e);
} else {
return new Seznam(e, s);
}
}
}
public static Elt find(Seznam s, int key){
if (s == null) {
return null;
}
comparisons++;
if (s.head.key == key) {
return s.head;
} else {
return find(s.tail, key);
}
}
public static Seznam delete(Seznam s, int key){
if (s == null) {
return null;
}
comparisons++;
if (s.head.key == key){
return s.tail;
} else {
return new Seznam(s.head, delete(s.tail, key));
}
}
public static void printElementKeys(Seznam s){
Seznam s1 = s;
while (s1 != null) {
System.out.println(s1.head.key);
s1 = s1.tail;
}
}
public static void printElementKeyComparisons(Seznam s) {
System.out.println(comparisons);
}
}