1
1
package de .akull .datastructures .linkedlist ;
2
2
3
+ /**
4
+ * Append
5
+ * Prepend
6
+ * Insert after
7
+ * Insert before
8
+ * Remove after
9
+ * Remove before
10
+ * Remove head
11
+ * Remove tail
12
+ * Remove node
13
+ * Get node at index
14
+ */
3
15
class LinkedList <T > {
4
16
5
17
Node <T > head ;
6
18
Node <T > tail ;
7
19
int size ;
8
20
21
+ /**
22
+ * Adds new node at the end of the list.
23
+ */
24
+ void append (Node <T > n ) {
25
+ if (head == null ) {
26
+ head = n ;
27
+ } else {
28
+ tail .next = n ;
29
+ }
30
+ tail = n ;
31
+ size ++;
32
+ }
33
+
34
+ /**
35
+ * Adds new node at the start of the list.
36
+ */
9
37
void prepend (Node <T > n ) {
10
38
n .next = head ;
11
39
head = n ;
12
40
13
41
if (tail == null ) {
14
- tail = head ;
15
- }
16
- size ++;
17
- }
18
-
19
- void append (Node <T > n ) {
20
- if (head == null ) {
21
- prepend (n );
22
- } else {
23
- tail .next = n ;
24
42
tail = n ;
25
- size ++;
26
43
}
44
+ size ++;
27
45
}
28
46
47
+ /**
48
+ * Adds a new node after a specific one.
49
+ */
29
50
void insertAfter (Node <T > node , Node <T > newNode ) {
30
51
if (node .equals (tail )) {
31
52
tail = newNode ;
@@ -35,6 +56,14 @@ void insertAfter(Node<T> node, Node<T> newNode) {
35
56
size ++;
36
57
}
37
58
59
+ /**
60
+ * Adds a new node before a specific one.
61
+ */
62
+ void insertBefore (Node <T > node , Node <T > newNode ) {}
63
+
64
+ /**
65
+ * Deletes node after a specific one.
66
+ */
38
67
void removeAfter (Node <T > n ) {
39
68
if (n .next != null ) {
40
69
if (n .next .equals (tail )) {
@@ -45,6 +74,14 @@ void removeAfter(Node<T> n) {
45
74
}
46
75
}
47
76
77
+ /**
78
+ * Deletes node before a specific one.
79
+ */
80
+ void removeBefore (Node <T > n ) {}
81
+
82
+ /**
83
+ * Deletes the current head.
84
+ */
48
85
void removeHead () {
49
86
if (head != null ) {
50
87
head = head .next ;
@@ -56,6 +93,19 @@ void removeHead() {
56
93
}
57
94
}
58
95
96
+ /**
97
+ * Deletes the current tail.
98
+ */
99
+ void removeTail () {}
100
+
101
+ /**
102
+ * Deletes a specific node.
103
+ */
104
+ void remove (Node <T > n ) {}
105
+
106
+ /**
107
+ * Returns a node at a specific position.
108
+ */
59
109
Node <T > get (int index ) {
60
110
if (index > size - 1 ) {
61
111
return null ;
0 commit comments