Kaynağa Gözat

simple-linked-list: simplify example implementation

Frida Johanne Tveit 9 yıl önce
ebeveyn
işleme
888b054e97

+ 13
- 13
exercises/simple-linked-list/src/example/java/SimpleLinkedList.java Dosyayı Görüntüle

3
 import java.util.NoSuchElementException;
3
 import java.util.NoSuchElementException;
4
 import java.util.Objects;
4
 import java.util.Objects;
5
 
5
 
6
-public class SimpleLinkedList<T> {
6
+class SimpleLinkedList<T> {
7
 
7
 
8
     private static class Element<T> {
8
     private static class Element<T> {
9
 
9
 
10
         final T value;
10
         final T value;
11
-        Element next;
11
+        Element<T> next;
12
 
12
 
13
         Element(T value) {
13
         Element(T value) {
14
             this.value = value;
14
             this.value = value;
18
     private Element<T> head;
18
     private Element<T> head;
19
     private int size;
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
         Element<T> newElement = new Element<>(value);
31
         Element<T> newElement = new Element<>(value);
32
         this.size++;
32
         this.size++;
33
         if (Objects.isNull(head)) {
33
         if (Objects.isNull(head)) {
38
         }
38
         }
39
     }
39
     }
40
 
40
 
41
-    public T pop() {
41
+    T pop() {
42
         if (Objects.isNull(head)) {
42
         if (Objects.isNull(head)) {
43
             throw new NoSuchElementException();
43
             throw new NoSuchElementException();
44
         }
44
         }
48
         return value;
48
         return value;
49
     }
49
     }
50
 
50
 
51
-    public void reverse() {
51
+    void reverse() {
52
         Element<T> current = head;
52
         Element<T> current = head;
53
         Element<T> next;
53
         Element<T> next;
54
         Element<T> previous = null;
54
         Element<T> previous = null;
61
         head = previous;
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
         int index = 0;
66
         int index = 0;
67
         Element<T> current = head;
67
         Element<T> current = head;
68
         while (Objects.nonNull(current)) {
68
         while (Objects.nonNull(current)) {
72
         return result;
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
         @SuppressWarnings("unchecked")
76
         @SuppressWarnings("unchecked")
77
         T[] arr = (T[]) Array.newInstance(clazz, size);
77
         T[] arr = (T[]) Array.newInstance(clazz, size);
78
 
78
 
79
         return arr;
79
         return arr;
80
     }
80
     }
81
 
81
 
82
-    public int size() {
82
+    int size() {
83
         return this.size;
83
         return this.size;
84
     }
84
     }
85
 }
85
 }