|
|
@@ -3,12 +3,12 @@ import java.lang.reflect.Array;
|
|
3
|
3
|
import java.util.NoSuchElementException;
|
|
4
|
4
|
import java.util.Objects;
|
|
5
|
5
|
|
|
6
|
|
-public class SimpleLinkedList<T> {
|
|
|
6
|
+class SimpleLinkedList<T> {
|
|
7
|
7
|
|
|
8
|
8
|
private static class Element<T> {
|
|
9
|
9
|
|
|
10
|
10
|
final T value;
|
|
11
|
|
- Element next;
|
|
|
11
|
+ Element<T> next;
|
|
12
|
12
|
|
|
13
|
13
|
Element(T value) {
|
|
14
|
14
|
this.value = value;
|
|
|
@@ -18,16 +18,16 @@ public class SimpleLinkedList<T> {
|
|
18
|
18
|
private Element<T> head;
|
|
19
|
19
|
private int size;
|
|
20
|
20
|
|
|
21
|
|
- public SimpleLinkedList() {
|
|
|
21
|
+ SimpleLinkedList() {
|
|
22
|
22
|
}
|
|
23
|
23
|
|
|
24
|
|
- public SimpleLinkedList(T[] values) {
|
|
25
|
|
- for (int ii = values.length - 1; ii >= 0; ii--) {
|
|
26
|
|
- push(values[ii]);
|
|
|
24
|
+ SimpleLinkedList(T[] values) {
|
|
|
25
|
+ for (int i = values.length - 1; i >= 0; i--) {
|
|
|
26
|
+ push(values[i]);
|
|
27
|
27
|
}
|
|
28
|
28
|
}
|
|
29
|
29
|
|
|
30
|
|
- public final void push(T value) {
|
|
|
30
|
+ final void push(T value) {
|
|
31
|
31
|
Element<T> newElement = new Element<>(value);
|
|
32
|
32
|
this.size++;
|
|
33
|
33
|
if (Objects.isNull(head)) {
|
|
|
@@ -38,7 +38,7 @@ public class SimpleLinkedList<T> {
|
|
38
|
38
|
}
|
|
39
|
39
|
}
|
|
40
|
40
|
|
|
41
|
|
- public T pop() {
|
|
|
41
|
+ T pop() {
|
|
42
|
42
|
if (Objects.isNull(head)) {
|
|
43
|
43
|
throw new NoSuchElementException();
|
|
44
|
44
|
}
|
|
|
@@ -48,7 +48,7 @@ public class SimpleLinkedList<T> {
|
|
48
|
48
|
return value;
|
|
49
|
49
|
}
|
|
50
|
50
|
|
|
51
|
|
- public void reverse() {
|
|
|
51
|
+ void reverse() {
|
|
52
|
52
|
Element<T> current = head;
|
|
53
|
53
|
Element<T> next;
|
|
54
|
54
|
Element<T> previous = null;
|
|
|
@@ -61,8 +61,8 @@ public class SimpleLinkedList<T> {
|
|
61
|
61
|
head = previous;
|
|
62
|
62
|
}
|
|
63
|
63
|
|
|
64
|
|
- public T[] asArray(Class<T> clazz) {
|
|
65
|
|
- T[] result = newArray(clazz, this.size);
|
|
|
64
|
+ T[] asArray(Class<T> clazz) {
|
|
|
65
|
+ T[] result = newArray(clazz, size);
|
|
66
|
66
|
int index = 0;
|
|
67
|
67
|
Element<T> current = head;
|
|
68
|
68
|
while (Objects.nonNull(current)) {
|
|
|
@@ -72,14 +72,14 @@ public class SimpleLinkedList<T> {
|
|
72
|
72
|
return result;
|
|
73
|
73
|
}
|
|
74
|
74
|
|
|
75
|
|
- private <T> T[] newArray(Class<T> clazz, int size) {
|
|
|
75
|
+ private T[] newArray(Class<T> clazz, int size) {
|
|
76
|
76
|
@SuppressWarnings("unchecked")
|
|
77
|
77
|
T[] arr = (T[]) Array.newInstance(clazz, size);
|
|
78
|
78
|
|
|
79
|
79
|
return arr;
|
|
80
|
80
|
}
|
|
81
|
81
|
|
|
82
|
|
- public int size() {
|
|
|
82
|
+ int size() {
|
|
83
|
83
|
return this.size;
|
|
84
|
84
|
}
|
|
85
|
85
|
}
|