6
6
package com .williamfiset .algorithms .datastructures .linkedlist ;
7
7
8
8
public class DoublyLinkedList <T > implements Iterable <T > {
9
-
10
9
private int size = 0 ;
11
10
private Node <T > head = null ;
12
11
private Node <T > tail = null ;
@@ -58,8 +57,6 @@ public void add(T elem) {
58
57
59
58
// Add a node to the tail of the linked list, O(1)
60
59
public void addLast (T elem ) {
61
-
62
- // The linked list is empty
63
60
if (isEmpty ()) {
64
61
head = tail = new Node <T >(elem , null , null );
65
62
} else {
@@ -71,15 +68,12 @@ public void addLast(T elem) {
71
68
72
69
// Add an element to the beginning of this linked list, O(1)
73
70
public void addFirst (T elem ) {
74
-
75
- // The linked list is empty
76
71
if (isEmpty ()) {
77
72
head = tail = new Node <T >(elem , null , null );
78
73
} else {
79
74
head .prev = new Node <T >(elem , null , head );
80
75
head = head .prev ;
81
76
}
82
-
83
77
size ++;
84
78
}
85
79
@@ -97,8 +91,7 @@ public T peekLast() {
97
91
98
92
// Remove the first value at the head of the linked list, O(1)
99
93
public T removeFirst () {
100
-
101
- // Can't remove data from an empty list -_-
94
+ // Can't remove data from an empty list
102
95
if (isEmpty ()) throw new RuntimeException ("Empty list" );
103
96
104
97
// Extract the data at the head and move
@@ -119,8 +112,7 @@ public T removeFirst() {
119
112
120
113
// Remove the last value at the tail of the linked list, O(1)
121
114
public T removeLast () {
122
-
123
- // Can't remove data from an empty list -_-
115
+ // Can't remove data from an empty list
124
116
if (isEmpty ()) throw new RuntimeException ("Empty list" );
125
117
126
118
// Extract the data at the tail and move
@@ -141,7 +133,6 @@ public T removeLast() {
141
133
142
134
// Remove an arbitrary node from the linked list, O(1)
143
135
private T remove (Node <T > node ) {
144
-
145
136
// If the node to remove is somewhere either at the
146
137
// head or the tail handle those independently
147
138
if (node .prev == null ) return removeFirst ();
@@ -167,25 +158,27 @@ private T remove(Node<T> node) {
167
158
// Remove a node at a particular index, O(n)
168
159
public T removeAt (int index ) {
169
160
170
- // Make sure the index provided is valid -_-
161
+ // Make sure the index provided is valid
171
162
if (index < 0 || index >= size ) throw new IllegalArgumentException ();
172
163
173
164
int i ;
174
165
Node <T > trav ;
175
166
176
167
// Search from the front of the list
177
168
if (index < size / 2 ) {
178
- for (i = 0 , trav = head ; i != index ; i ++) trav = trav .next ;
179
-
169
+ for (i = 0 , trav = head ; i != index ; i ++) {
170
+ trav = trav .next ;
171
+ }
180
172
// Search from the back of the list
181
- } else for (i = size - 1 , trav = tail ; i != index ; i --) trav = trav .prev ;
173
+ } else for (i = size - 1 , trav = tail ; i != index ; i --) {
174
+ trav = trav .prev ;
175
+ }
182
176
183
177
return remove (trav );
184
178
}
185
179
186
180
// Remove a particular value in the linked list, O(n)
187
181
public boolean remove (Object obj ) {
188
-
189
182
Node <T > trav = head ;
190
183
191
184
// Support searching for null
@@ -210,16 +203,22 @@ public boolean remove(Object obj) {
210
203
211
204
// Find the index of a particular value in the linked list, O(n)
212
205
public int indexOf (Object obj ) {
213
-
214
206
int index = 0 ;
215
207
Node <T > trav = head ;
216
208
217
209
// Support searching for null
218
210
if (obj == null ) {
219
- for (; trav != null ; trav = trav .next , index ++) if (trav .data == null ) return index ;
220
-
211
+ for (; trav != null ; trav = trav .next , index ++) {
212
+ if (trav .data == null ) {
213
+ return index ;
214
+ }
215
+ }
221
216
// Search for non null object
222
- } else for (; trav != null ; trav = trav .next , index ++) if (obj .equals (trav .data )) return index ;
217
+ } else for (; trav != null ; trav = trav .next , index ++) {
218
+ if (obj .equals (trav .data )) {
219
+ return index ;
220
+ }
221
+ }
223
222
224
223
return -1 ;
225
224
}
0 commit comments