Skip to content

Commit e300d8b

Browse files
committed
Google(R) Java Format(TM) everything
1 parent 38a7fba commit e300d8b

9 files changed

Lines changed: 89 additions & 114 deletions

File tree

src/main/lists/ArrayQueue.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
/**
22
* An implementation of queue using a static array.
33
*
4-
* <p>
5-
* This implementation uses the cyclic buffer by storing the indices of the rear
6-
* and the front of the queue to achieve O(1) complexity on all operations.
4+
* <p>This implementation uses the cyclic buffer by storing the indices of the rear and the front of
5+
* the queue to achieve O(1) complexity on all operations.
76
*
8-
* <p>
9-
* Operations and their time complexities are:
10-
* <ul>
11-
* <li><code>enqueue(T)</code>: O(1)</li>
12-
* <li><code>dequeue()</code>: O(1)</li>
13-
* <li><code>peek()</code>: O(1)</li>
14-
* </ul>
7+
* <p>Operations and their time complexities are:
8+
*
9+
* <ul>
10+
* <li><code>enqueue(T)</code>: O(1)
11+
* <li><code>dequeue()</code>: O(1)
12+
* <li><code>peek()</code>: O(1)
13+
* </ul>
1514
*/
1615
public class ArrayQueue<T> implements Queue<T> {
1716
Object[] array;
@@ -40,8 +39,7 @@ public boolean isEmpty() {
4039

4140
@SuppressWarnings("unchecked")
4241
public T enqueue(T data) {
43-
if (this.length == this.array.length)
44-
return null;
42+
if (this.length == this.array.length) return null;
4543

4644
this.rearIndex = (this.rearIndex + 1) % this.array.length;
4745
this.array[this.rearIndex] = data;
@@ -52,8 +50,7 @@ public T enqueue(T data) {
5250

5351
@SuppressWarnings("unchecked")
5452
public T dequeue() {
55-
if (this.length == 0)
56-
return null;
53+
if (this.length == 0) return null;
5754

5855
T dequeuedData = (T) this.array[this.frontIndex];
5956
this.array[this.frontIndex] = null;

src/main/lists/ArrayStack.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/**
22
* An implementation of stack using static array.
33
*
4-
* <p>
5-
* Operations with their time complexities are:
6-
* <ul>
7-
* <li><code>push(T)</code>: O(1)</li>
8-
* <li><code>pop()</code>: O(1)</li>
9-
* <li><code>peek()</code>: O(1)</li>
10-
* </ul>
4+
* <p>Operations with their time complexities are:
115
*
6+
* <ul>
7+
* <li><code>push(T)</code>: O(1)
8+
* <li><code>pop()</code>: O(1)
9+
* <li><code>peek()</code>: O(1)
10+
* </ul>
1211
*/
1312
public class ArrayStack<T> implements Stack<T> {
1413
Object[] array;
@@ -29,17 +28,15 @@ public int getCapacity() {
2928

3029
@SuppressWarnings("unchecked")
3130
public T push(T data) {
32-
if (this.length == this.array.length)
33-
return null;
31+
if (this.length == this.array.length) return null;
3432

3533
this.array[this.length] = data;
3634
return (T) this.array[this.length++];
3735
}
3836

3937
@SuppressWarnings("unchecked")
4038
public T pop() {
41-
if (this.length == 0)
42-
return null;
39+
if (this.length == 0) return null;
4340

4441
T poppedData = (T) this.array[--this.length];
4542
this.array[this.length] = null;

src/main/lists/DynamicArray.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/**
22
* A dynamic array implementation based on a fixed-size array.
33
*
4-
* <p>
5-
* Operations with their time complexities are:
6-
* <ul>
7-
* <li><code>shrinkCapacity()</code>: O(n)</li>
8-
* <li><code>growCapacity()</code>: O(n)</li>
9-
* <li><code>append(T)</code>: O(1)</li>
10-
* <li><code>insert(T, int)</code>: O(n)</li>
11-
* <li><code>remove()</code>: O(1)</li>
12-
* <li><code>remove(int)</code>: O(n)</li>
13-
* <li><code>get</code>: O(1)</li>
14-
* </ul>
4+
* <p>Operations with their time complexities are:
5+
*
6+
* <ul>
7+
* <li><code>shrinkCapacity()</code>: O(n)
8+
* <li><code>growCapacity()</code>: O(n)
9+
* <li><code>append(T)</code>: O(1)
10+
* <li><code>insert(T, int)</code>: O(n)
11+
* <li><code>remove()</code>: O(1)
12+
* <li><code>remove(int)</code>: O(n)
13+
* <li><code>get</code>: O(1)
14+
* </ul>
1515
*/
1616
public class DynamicArray<T> {
1717
Object[] array; // The underlying fixed-size array
@@ -38,8 +38,7 @@ public int getCapacity() {
3838
public DynamicArray<T> shrinkCapacity() {
3939
Object[] newArray = new Object[this.length];
4040

41-
for (int i = 0; i < this.length; i++)
42-
newArray[i] = this.array[i];
41+
for (int i = 0; i < this.length; i++) newArray[i] = this.array[i];
4342

4443
this.array = newArray;
4544
return this;
@@ -48,27 +47,23 @@ public DynamicArray<T> shrinkCapacity() {
4847
public DynamicArray<T> growCapacity() {
4948
Object[] newArray = new Object[this.array.length * 2];
5049

51-
for (int i = 0; i < this.length; i++)
52-
newArray[i] = this.array[i];
50+
for (int i = 0; i < this.length; i++) newArray[i] = this.array[i];
5351

5452
this.array = newArray;
5553
return this;
5654
}
5755

5856
public DynamicArray<T> append(T data) {
59-
if (this.length + 1 > this.array.length)
60-
this.growCapacity();
57+
if (this.length + 1 > this.array.length) this.growCapacity();
6158

6259
this.array[this.length++] = data;
6360
return this;
6461
}
6562

6663
public DynamicArray<T> insert(T data, int index) {
67-
if (this.length + 1 > this.array.length)
68-
this.growCapacity();
64+
if (this.length + 1 > this.array.length) this.growCapacity();
6965

70-
for (int i = this.length++; i > index; i--)
71-
this.array[i] = this.array[i - 1];
66+
for (int i = this.length++; i > index; i--) this.array[i] = this.array[i - 1];
7267

7368
this.array[index] = data;
7469
return this;
@@ -80,8 +75,7 @@ public DynamicArray<T> remove() {
8075
}
8176

8277
public DynamicArray<T> remove(int index) {
83-
for (int i = index + 1; i < this.length; i++)
84-
this.array[i - 1] = this.array[i];
78+
for (int i = index + 1; i < this.length; i++) this.array[i - 1] = this.array[i];
8579

8680
return this.remove();
8781
}

src/main/lists/LinkedList.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
/**
44
* Simple linked list implementation.
55
*
6-
* <p>
7-
* Operations with their complexities are:
8-
* <ul>
9-
* <li><code>get(int)</code>: O(n)</li>
10-
* <li><code>insert(T, int)</code>: O(n)</li>
11-
* <li><code>insertAtHead(T)</code>: O(1)</li>
12-
* <li><code>delete(int)</code>: O(n)</li>
13-
* <li><code>deleteAtHead()</code>: O(1)</li>
14-
* </ul>
6+
* <p>Operations with their complexities are:
7+
*
8+
* <ul>
9+
* <li><code>get(int)</code>: O(n)
10+
* <li><code>insert(T, int)</code>: O(n)
11+
* <li><code>insertAtHead(T)</code>: O(1)
12+
* <li><code>delete(int)</code>: O(n)
13+
* <li><code>deleteAtHead()</code>: O(1)
14+
* </ul>
1515
*/
1616
public class LinkedList<T> {
1717
LinkedNode<T> head;
@@ -48,25 +48,21 @@ public boolean isEmpty() {
4848
}
4949

5050
public T get(int index) {
51-
if (index < 0 || index >= this.length)
52-
throw new IndexOutOfBoundsException();
51+
if (index < 0 || index >= this.length) throw new IndexOutOfBoundsException();
5352

5453
LinkedNode<T> node = this.head.next;
55-
for (int i = 0; i < index; i++)
56-
node = node.next;
54+
for (int i = 0; i < index; i++) node = node.next;
5755

5856
return node.data;
5957
}
6058

6159
public LinkedNode<T> insert(T data, int index) {
62-
if (index < 0 || index > this.length)
63-
throw new IndexOutOfBoundsException();
60+
if (index < 0 || index > this.length) throw new IndexOutOfBoundsException();
6461

6562
LinkedNode<T> newNode = new LinkedNode<T>(data, null);
6663
LinkedNode<T> prevNode = this.head;
6764

68-
for (int i = 0; i < index; i++)
69-
prevNode = prevNode.next;
65+
for (int i = 0; i < index; i++) prevNode = prevNode.next;
7066

7167
if (index != this.length) newNode.next = prevNode.next;
7268
prevNode.next = newNode;
@@ -84,13 +80,11 @@ public LinkedNode<T> insertAtHead(T data) {
8480
}
8581

8682
public LinkedNode<T> delete(int index) {
87-
if (index < 0 || index >= this.length)
88-
throw new IndexOutOfBoundsException();
83+
if (index < 0 || index >= this.length) throw new IndexOutOfBoundsException();
8984

9085
LinkedNode<T> prevNode = this.head;
9186

92-
for (int i = 0; i < index; i++)
93-
prevNode = prevNode.next;
87+
for (int i = 0; i < index; i++) prevNode = prevNode.next;
9488

9589
LinkedNode<T> deletedNode = prevNode.next;
9690
if (index != this.length) prevNode.next = deletedNode.next;

src/main/lists/LinkedListAlgorithms.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
public class LinkedListAlgorithms {
22
/**
3-
* Detects the cycle, if exists, in a given linked list, using Floyd's
4-
* turtoise and hare algorithm, or two-pointer technique.
3+
* Detects the cycle, if exists, in a given linked list, using Floyd's turtoise and hare
4+
* algorithm, or two-pointer technique.
55
*
6-
* <p>
7-
* Runs in O(n) time and O(1) memory.
6+
* <p>Runs in O(n) time and O(1) memory.
87
*
98
* @param list
109
* @return The first node in the cycle.
@@ -35,25 +34,20 @@ public static <T> LinkedNode<T> detectCycle(LinkedList<T> list) {
3534
/**
3635
* Returns the intersection of two linked lists.
3736
*
38-
* <p>
39-
* Given lists A and B, first starts at the head of list B and travels to the
40-
* end (the last node after the intersection), which we call <code>tail</code>.
41-
* Then routes tail to A's head to form a cycle. Proceeds to use Floyd's
42-
* turtoise and hare algorithm starting from B's head.
37+
* <p>Given lists A and B, first starts at the head of list B and travels to the end (the last
38+
* node after the intersection), which we call <code>tail</code>. Then routes tail to A's head to
39+
* form a cycle. Proceeds to use Floyd's turtoise and hare algorithm starting from B's head.
40+
*
41+
* <p>Runs in linear time and constant memory.
4342
*
44-
* <p>
45-
* Runs in linear time and constant memory.
46-
*
4743
* @param listA
4844
* @param listB
49-
* @return The intersection of two given lists, or <code>null</code> if there
50-
* is no intersection.
45+
* @return The intersection of two given lists, or <code>null</code> if there is no intersection.
5146
*/
5247
public static <T> LinkedNode<T> getIntersection(LinkedList<T> listA, LinkedList<T> listB) {
5348
LinkedNode<T> tail = listB.getHead(); // The last node after the intersection (if exists)
5449

55-
while (tail.next != null)
56-
tail = tail.next;
50+
while (tail.next != null) tail = tail.next;
5751

5852
tail.next = listA.getHead();
5953
LinkedNode<T> intersection = detectCycle(listB);
@@ -64,11 +58,11 @@ public static <T> LinkedNode<T> getIntersection(LinkedList<T> listA, LinkedList<
6458

6559
/**
6660
* Reverse a given linked list in-place.
67-
*
61+
*
6862
* @param list
6963
* @return The given linked list that has been reversed.
7064
*/
71-
public static<T> LinkedList<T> reverse(LinkedList<T> list) {
65+
public static <T> LinkedList<T> reverse(LinkedList<T> list) {
7266
LinkedNode<T> next;
7367
LinkedNode<T> cur;
7468
LinkedNode<T> prev = list.getHead();

src/main/lists/LinkedQueue.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
/**
22
* An implementation of the queue data structure based on linked list.
33
*
4-
* <p>
5-
* This implementation is basically a linked list with a tail, where the head
6-
* of the list is the front of the queue and the tail is the rear. Both the
7-
* front and the rear are empty nodes themselves. The last enqueued element
8-
* points to the rear and the rear points back to the element, forming a little
9-
* cycle. This helps achieve both enqueueing and dequeueing in constant time.
4+
* <p>This implementation is basically a linked list with a tail, where the head of the list is the
5+
* front of the queue and the tail is the rear. Both the front and the rear are empty nodes
6+
* themselves. The last enqueued element points to the rear and the rear points back to the element,
7+
* forming a little cycle. This helps achieve both enqueueing and dequeueing in constant time.
108
*
11-
* <p>
12-
* Operations with their complexities are:
13-
* <ul>
14-
* <li><code>enqueue(T)</code>: O(1)</li>
15-
* <li><code>dequeue()</code>: O(1)</li>
16-
* <li><code>peek()</code>: O(1)</li>
17-
* </ul>
9+
* <p>Operations with their complexities are:
10+
*
11+
* <ul>
12+
* <li><code>enqueue(T)</code>: O(1)
13+
* <li><code>dequeue()</code>: O(1)
14+
* <li><code>peek()</code>: O(1)
15+
* </ul>
1816
*/
1917
public class LinkedQueue<T> implements Queue<T> {
2018
private LinkedNode<T> front; // head of linked list
@@ -44,8 +42,7 @@ public T enqueue(T data) {
4442
}
4543

4644
public T dequeue() {
47-
if (this.length == 0)
48-
return null;
45+
if (this.length == 0) return null;
4946

5047
T dequeuedData = this.front.next.data;
5148
this.front.next = this.front.next.next;

src/main/lists/LinkedStack.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
/**
22
* An implementation of a stack based on a linked list.
33
*
4-
* <p>
5-
* The head of the linked list is the top of the stack. All operations are O(1)
6-
* in time and space complexity.
4+
* <p>The head of the linked list is the top of the stack. All operations are O(1) in time and space
5+
* complexity.
76
*
8-
* <p>
9-
* Operations with their time complexities are:
10-
* <ul>
11-
* <li><code>push(T)</code>: O(1)</li>
12-
* <li><code>pop()</code>: O(1)</li>
13-
* <li><code>peek()</code>: O(1)</li>
14-
* </ul>
7+
* <p>Operations with their time complexities are:
8+
*
9+
* <ul>
10+
* <li><code>push(T)</code>: O(1)
11+
* <li><code>pop()</code>: O(1)
12+
* <li><code>peek()</code>: O(1)
13+
* </ul>
1514
*/
1615
public class LinkedStack<T> implements Stack<T> {
1716
private LinkedNode<T> top;
@@ -38,8 +37,7 @@ public T push(T data) {
3837
}
3938

4039
public T pop() {
41-
if (this.length == 0)
42-
return null;
40+
if (this.length == 0) return null;
4341

4442
T poppedData = this.top.next.data;
4543
this.top.next = this.top.next.next;

src/main/lists/Queue.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
public interface Queue<T> {
22
public T enqueue(T data);
3+
34
public T dequeue();
5+
46
public T peek();
57
}

src/main/lists/Stack.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
public interface Stack<T> {
22
public T push(T data);
3+
34
public T pop();
5+
46
public T peek();
57
}

0 commit comments

Comments
 (0)