Skip to content

Commit fee9632

Browse files
committed
formatting
1 parent 05dbe78 commit fee9632

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

com/williamfiset/algorithms/datastructures/linkedlist/DoublyLinkedList.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package com.williamfiset.algorithms.datastructures.linkedlist;
77

88
public class DoublyLinkedList<T> implements Iterable<T> {
9-
109
private int size = 0;
1110
private Node<T> head = null;
1211
private Node<T> tail = null;
@@ -58,8 +57,6 @@ public void add(T elem) {
5857

5958
// Add a node to the tail of the linked list, O(1)
6059
public void addLast(T elem) {
61-
62-
// The linked list is empty
6360
if (isEmpty()) {
6461
head = tail = new Node<T>(elem, null, null);
6562
} else {
@@ -71,15 +68,12 @@ public void addLast(T elem) {
7168

7269
// Add an element to the beginning of this linked list, O(1)
7370
public void addFirst(T elem) {
74-
75-
// The linked list is empty
7671
if (isEmpty()) {
7772
head = tail = new Node<T>(elem, null, null);
7873
} else {
7974
head.prev = new Node<T>(elem, null, head);
8075
head = head.prev;
8176
}
82-
8377
size++;
8478
}
8579

@@ -97,8 +91,7 @@ public T peekLast() {
9791

9892
// Remove the first value at the head of the linked list, O(1)
9993
public T removeFirst() {
100-
101-
// Can't remove data from an empty list -_-
94+
// Can't remove data from an empty list
10295
if (isEmpty()) throw new RuntimeException("Empty list");
10396

10497
// Extract the data at the head and move
@@ -119,8 +112,7 @@ public T removeFirst() {
119112

120113
// Remove the last value at the tail of the linked list, O(1)
121114
public T removeLast() {
122-
123-
// Can't remove data from an empty list -_-
115+
// Can't remove data from an empty list
124116
if (isEmpty()) throw new RuntimeException("Empty list");
125117

126118
// Extract the data at the tail and move
@@ -141,7 +133,6 @@ public T removeLast() {
141133

142134
// Remove an arbitrary node from the linked list, O(1)
143135
private T remove(Node<T> node) {
144-
145136
// If the node to remove is somewhere either at the
146137
// head or the tail handle those independently
147138
if (node.prev == null) return removeFirst();
@@ -167,25 +158,27 @@ private T remove(Node<T> node) {
167158
// Remove a node at a particular index, O(n)
168159
public T removeAt(int index) {
169160

170-
// Make sure the index provided is valid -_-
161+
// Make sure the index provided is valid
171162
if (index < 0 || index >= size) throw new IllegalArgumentException();
172163

173164
int i;
174165
Node<T> trav;
175166

176167
// Search from the front of the list
177168
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+
}
180172
// 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+
}
182176

183177
return remove(trav);
184178
}
185179

186180
// Remove a particular value in the linked list, O(n)
187181
public boolean remove(Object obj) {
188-
189182
Node<T> trav = head;
190183

191184
// Support searching for null
@@ -210,16 +203,22 @@ public boolean remove(Object obj) {
210203

211204
// Find the index of a particular value in the linked list, O(n)
212205
public int indexOf(Object obj) {
213-
214206
int index = 0;
215207
Node<T> trav = head;
216208

217209
// Support searching for null
218210
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+
}
221216
// 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+
}
223222

224223
return -1;
225224
}

0 commit comments

Comments
 (0)