Skip to content

Commit 38a7fba

Browse files
committed
API and names update
1 parent ab5f675 commit 38a7fba

11 files changed

Lines changed: 225 additions & 214 deletions

src/main/lists/ArrayQueue.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
* <p>
99
* Operations and their time complexities are:
1010
* <ul>
11-
* <li><code>enqueue</code>: O(1)</li>
12-
* <li><code>dequeue</code>: O(1)</li>
13-
* <li><code>peek</code>: O(1)</li>
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>
1414
* </ul>
1515
*/
16-
public class ArrayQueue<T> {
17-
private Object[] array;
18-
private int length;
19-
private int frontIndex;
20-
private int rearIndex;
16+
public class ArrayQueue<T> implements Queue<T> {
17+
Object[] array;
18+
int length;
19+
int frontIndex;
20+
int rearIndex;
2121

2222
public ArrayQueue(int capacity) {
2323
this.array = new Object[capacity];
@@ -34,26 +34,33 @@ public int getCapacity() {
3434
return this.array.length;
3535
}
3636

37-
public ArrayQueue<T> enqueue(T data) {
37+
public boolean isEmpty() {
38+
return this.length == 0;
39+
}
40+
41+
@SuppressWarnings("unchecked")
42+
public T enqueue(T data) {
3843
if (this.length == this.array.length)
39-
return this;
44+
return null;
4045

4146
this.rearIndex = (this.rearIndex + 1) % this.array.length;
4247
this.array[this.rearIndex] = data;
4348

4449
this.length++;
45-
return this;
50+
return (T) this.array[this.rearIndex];
4651
}
4752

48-
public ArrayQueue<T> dequeue() {
53+
@SuppressWarnings("unchecked")
54+
public T dequeue() {
4955
if (this.length == 0)
50-
return this;
56+
return null;
5157

58+
T dequeuedData = (T) this.array[this.frontIndex];
5259
this.array[this.frontIndex] = null;
5360
this.frontIndex = (this.frontIndex + 1) % this.array.length;
5461

5562
this.length--;
56-
return this;
63+
return dequeuedData;
5764
}
5865

5966
@SuppressWarnings("unchecked")

src/main/lists/ArrayStack.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* <p>
55
* Operations with their time complexities are:
66
* <ul>
7-
* <li><code>push</code>: O(1)</li>
8-
* <li><code>pop</code>: O(1)</li>
9-
* <li><code>peek</code>: O(1)</li>
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>
1010
* </ul>
1111
*
1212
*/
13-
public class ArrayStack<T> {
14-
private Object[] array;
15-
private int length;
13+
public class ArrayStack<T> implements Stack<T> {
14+
Object[] array;
15+
int length;
1616

1717
public ArrayStack(int capacity) {
1818
this.array = new Object[capacity];
@@ -27,24 +27,27 @@ public int getCapacity() {
2727
return this.array.length;
2828
}
2929

30-
public ArrayStack<T> push(T data) {
30+
@SuppressWarnings("unchecked")
31+
public T push(T data) {
3132
if (this.length == this.array.length)
32-
return this;
33+
return null;
3334

34-
this.array[this.length++] = data;
35-
return this;
35+
this.array[this.length] = data;
36+
return (T) this.array[this.length++];
3637
}
3738

38-
public ArrayStack<T> pop() {
39+
@SuppressWarnings("unchecked")
40+
public T pop() {
3941
if (this.length == 0)
40-
return this;
42+
return null;
4143

42-
this.array[--this.length] = null;
43-
return this;
44+
T poppedData = (T) this.array[--this.length];
45+
this.array[this.length] = null;
46+
return poppedData;
4447
}
4548

4649
@SuppressWarnings("unchecked")
4750
public T peek() {
48-
return (this.length == 0) ? null : (T) this.array[this.length - 1];
51+
return (T) this.array[(this.length - 1) % (this.length + 1)];
4952
}
5053
}

src/main/lists/DynamicArray.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
* <p>
55
* Operations with their time complexities are:
66
* <ul>
7-
* <li><code>shrinkCapacity</code>: O(n)</li>
8-
* <li><code>growCapacity</code>: O(n)</li>
9-
* <li><code>append</code>: O(1)</li>
10-
* <li><code>insert</code>: O(n)</li>
11-
* <li><code>remove</code> (at last element): O(1)</li>
12-
* <li><code>remove</code> (at specified index): O(n)</li>
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>
1313
* <li><code>get</code>: O(1)</li>
1414
* </ul>
1515
*/
1616
public class DynamicArray<T> {
17-
private Object[] array; // The underlying fixed-size array
18-
private int length; // The number of elements in this array
17+
Object[] array; // The underlying fixed-size array
18+
int length; // The number of elements in this array
1919

2020
public DynamicArray() {
2121
this.array = new Object[10];

src/main/lists/LinkedList.java

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
* <p>
77
* Operations with their complexities are:
88
* <ul>
9-
* <li><code>get</code>: O(n)</li>
10-
* <li><code>insert</code>: O(n)</li>
11-
* <li><code>insertAtHead</code>: O(1)</li>
12-
* <li><code>delete</code>: O(n)</li>
13-
* <li><code>deleteAtHead</code>: O(1)</li>
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>
1414
* </ul>
1515
*/
1616
public class LinkedList<T> {
17-
private LinkedListNode<T> head;
18-
private int length;
17+
LinkedNode<T> head;
18+
int length;
1919

2020
@SuppressWarnings("unchecked")
21-
public static <T> T[] toArray(LinkedListNode<T> head, int length, Class<T> c) {
21+
public static <T> T[] toArray(LinkedNode<T> head, int length, Class<T> c) {
2222
T[] array = (T[]) Array.newInstance(c, length);
23-
LinkedListNode<T> node = head;
23+
LinkedNode<T> node = head;
2424

2525
for (int i = 0; i < length; i++) {
2626
node = node.next;
@@ -31,19 +31,11 @@ public static <T> T[] toArray(LinkedListNode<T> head, int length, Class<T> c) {
3131
}
3232

3333
public LinkedList() {
34-
this.head = new LinkedListNode<T>(null, null);
34+
this.head = new LinkedNode<T>(null, null);
3535
this.length = 0;
3636
}
3737

38-
public LinkedList(T[] data) {
39-
this.head = new LinkedListNode<T>(null, null);
40-
this.length = 0;
41-
42-
for (T d: data)
43-
this.insert(d, this.length);
44-
}
45-
46-
public LinkedListNode<T> getHead() {
38+
public LinkedNode<T> getHead() {
4739
return this.head.next;
4840
}
4941

@@ -56,21 +48,22 @@ public boolean isEmpty() {
5648
}
5749

5850
public T get(int index) {
59-
if (index < 0 || index >= this.length) throw new IndexOutOfBoundsException();
60-
61-
LinkedListNode<T> node = this.head.next;
51+
if (index < 0 || index >= this.length)
52+
throw new IndexOutOfBoundsException();
6253

54+
LinkedNode<T> node = this.head.next;
6355
for (int i = 0; i < index; i++)
6456
node = node.next;
6557

6658
return node.data;
6759
}
6860

69-
public LinkedListNode<T> insert(T data, int index) {
70-
if (index < 0 || index > this.length) throw new IndexOutOfBoundsException();
61+
public LinkedNode<T> insert(T data, int index) {
62+
if (index < 0 || index > this.length)
63+
throw new IndexOutOfBoundsException();
7164

72-
LinkedListNode<T> newNode = new LinkedListNode<T>(data, null);
73-
LinkedListNode<T> prevNode = this.head;
65+
LinkedNode<T> newNode = new LinkedNode<T>(data, null);
66+
LinkedNode<T> prevNode = this.head;
7467

7568
for (int i = 0; i < index; i++)
7669
prevNode = prevNode.next;
@@ -82,32 +75,33 @@ public LinkedListNode<T> insert(T data, int index) {
8275
return newNode;
8376
}
8477

85-
public LinkedListNode<T> insertAtHead(T data) {
86-
LinkedListNode<T> newNode = new LinkedListNode<T>(data, this.head.next);
78+
public LinkedNode<T> insertAtHead(T data) {
79+
LinkedNode<T> newNode = new LinkedNode<T>(data, this.head.next);
8780
this.head.next = newNode;
8881

8982
this.length++;
9083
return newNode;
9184
}
9285

93-
public LinkedListNode<T> delete(int index) {
94-
if (index < 0 || index >= this.length) throw new IndexOutOfBoundsException();
86+
public LinkedNode<T> delete(int index) {
87+
if (index < 0 || index >= this.length)
88+
throw new IndexOutOfBoundsException();
9589

96-
LinkedListNode<T> prevNode = this.head;
90+
LinkedNode<T> prevNode = this.head;
9791

9892
for (int i = 0; i < index; i++)
9993
prevNode = prevNode.next;
10094

101-
LinkedListNode<T> deletedNode = prevNode.next;
95+
LinkedNode<T> deletedNode = prevNode.next;
10296
if (index != this.length) prevNode.next = deletedNode.next;
10397

10498
this.length--;
10599
return deletedNode;
106100
}
107101

108-
public LinkedListNode<T> deleteAtHead() {
102+
public LinkedNode<T> deleteAtHead() {
109103
if (this.head.next != null) {
110-
LinkedListNode<T> deletedNode = this.head.next;
104+
LinkedNode<T> deletedNode = this.head.next;
111105
this.head.next = this.head.next.next;
112106

113107
this.length--;

src/main/lists/LinkedListAlgorithms.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ public class LinkedListAlgorithms {
99
* @param list
1010
* @return The first node in the cycle.
1111
*/
12-
public static <T> LinkedListNode<T> detectCycle(LinkedList<T> list) {
13-
LinkedListNode<T> rabbit = list.getHead();
14-
LinkedListNode<T> turtle = list.getHead();
12+
public static <T> LinkedNode<T> detectCycle(LinkedList<T> list) {
13+
LinkedNode<T> rabbit = list.getHead();
14+
LinkedNode<T> turtle = list.getHead();
1515

1616
while (rabbit != null && rabbit.next != null) {
1717
rabbit = rabbit.next.next;
1818
turtle = turtle.next;
1919

2020
if (rabbit == turtle) {
21-
LinkedListNode<T> bear = list.getHead();
21+
LinkedNode<T> bear = list.getHead();
2222

2323
while (bear != rabbit) {
2424
bear = bear.next;
@@ -49,16 +49,28 @@ public static <T> LinkedListNode<T> detectCycle(LinkedList<T> list) {
4949
* @return The intersection of two given lists, or <code>null</code> if there
5050
* is no intersection.
5151
*/
52-
public static <T> LinkedListNode<T> getIntersection(LinkedList<T> listA, LinkedList<T> listB) {
53-
LinkedListNode<T> tail = listB.getHead(); // The last node after the intersection (if exists)
52+
public static <T> LinkedNode<T> getIntersection(LinkedList<T> listA, LinkedList<T> listB) {
53+
LinkedNode<T> tail = listB.getHead(); // The last node after the intersection (if exists)
5454

5555
while (tail.next != null)
5656
tail = tail.next;
5757

5858
tail.next = listA.getHead();
59-
LinkedListNode<T> intersection = detectCycle(listB);
59+
LinkedNode<T> intersection = detectCycle(listB);
6060

6161
tail.next = null;
6262
return intersection;
6363
}
64+
65+
/**
66+
* Reverse a given linked list in-place.
67+
*
68+
* @param list
69+
* @return The given linked list that has been reversed.
70+
*/
71+
public static<T> LinkedList<T> reverse(LinkedList<T> list) {
72+
LinkedNode<T> next;
73+
LinkedNode<T> cur;
74+
LinkedNode<T> prev = list.getHead();
75+
}
6476
}

src/main/lists/LinkedListNode.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/lists/LinkedNode.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class LinkedNode<T> {
2+
public T data;
3+
protected LinkedNode<T> next;
4+
5+
protected LinkedNode(T data, LinkedNode<T> next) {
6+
this.data = data;
7+
this.next = next;
8+
}
9+
}

0 commit comments

Comments
 (0)