Skip to content

Commit 7c6e1f0

Browse files
committed
Finish LinkedList ops, ready to upgrade to generics
1 parent 4f65ab9 commit 7c6e1f0

1 file changed

Lines changed: 125 additions & 26 deletions

File tree

src/main/LinkedList.java

Lines changed: 125 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class LinkedListNode {
1+
class LinkedListNode {
22
public byte data;
33
protected LinkedListNode next;
44

@@ -8,13 +8,44 @@ public LinkedListNode(byte data, LinkedListNode next) {
88
}
99
}
1010

11+
/**
12+
* Simple linked list implementation.
13+
*
14+
* <p>
15+
* Operations with their complexities are:
16+
* <ul>
17+
* <li>Get data at an index: O(n)</li>
18+
* <li>Insert data at head: O(1)</li>
19+
* <li>Insert data at an index: O(n)</li>
20+
* <li>Delete data at an index: O(n)</li>
21+
* </ul>
22+
*/
1123
public class LinkedList {
24+
/**
25+
* The head of the linked list which is a <code>LinkedListNode</code> that
26+
* points to the first node of the linked list.
27+
*
28+
* <p>
29+
* Having the head being an empty node rather than the first node in the
30+
* actual list helps clean the code and reduces if statements.
31+
*/
1232
private LinkedListNode head;
33+
/**
34+
* The number of nodes in this linked list, excluding the head.
35+
*/
1336
private int length;
1437

1538
public LinkedList() {
16-
this.head = null;
39+
this.head = new LinkedListNode((byte) 0, null);
40+
this.length = 0;
41+
}
42+
43+
public LinkedList(byte[] data) {
44+
this.head = new LinkedListNode((byte) 0, null);
1745
this.length = 0;
46+
47+
for (byte d: data)
48+
this.insert(d, this.length);
1849
}
1950

2051
public LinkedListNode getHead() {
@@ -26,61 +57,129 @@ public int getLength() {
2657
}
2758

2859
/**
29-
* Insert new data at a specified index, shifting existing node at the index
60+
* Checks if this list is empty.
61+
*
62+
* @return <code>true</code> if this list is empty, <code>false</code> if not.
63+
*/
64+
public boolean isEmpty() {
65+
return this.length == 0;
66+
}
67+
68+
/**
69+
* Returns the data contained by the node at the specified index.
70+
*
71+
* @param index
72+
* @return The data stored at the index.
73+
*/
74+
public byte get(int index) {
75+
if (index < 0 || index >= this.length) throw new IndexOutOfBoundsException();
76+
77+
LinkedListNode node = this.head.next;
78+
79+
for (int i = 0; i < index; i++)
80+
node = node.next;
81+
82+
return node.data;
83+
}
84+
85+
/**
86+
* Inserts new data at the head.
87+
*
88+
* <p>
89+
* O(1) complexity.
90+
*
91+
* @param data
92+
* @return The new node inserted that contains the data.
93+
*/
94+
public LinkedListNode insertAtHead(byte data) {
95+
LinkedListNode newNode = new LinkedListNode(data, this.head.next);
96+
this.head.next = newNode;
97+
98+
this.length++;
99+
return newNode;
100+
}
101+
102+
/**
103+
* Inserts new data at a specified index, shifting existing node at the index
30104
* and all nodes after it to the right.
31105
*
32106
* <p>
33-
* O(1) complexity for inserting at the head, O(i) complexity for other cases.
107+
* O(n) complexity.
108+
*
34109
* @param data
35110
* @param index A valid index for the new node that is between 0 and
36111
* <code>this.length</code> (inclusive).
37-
* @return
38-
*/
112+
* @return The new node inserted that contains the data.
113+
*/
39114
public LinkedListNode insert(byte data, int index) {
40-
if (index < 0 || index > this.length) return null;
115+
if (index < 0 || index > this.length) throw new IndexOutOfBoundsException();
41116

42117
LinkedListNode newNode = new LinkedListNode(data, null);
43-
44-
if (index == 0) {
45-
if (this.head != null) newNode.next = this.head;
46-
this.head = newNode;
47-
48-
return newNode;
49-
}
50-
51118
LinkedListNode prevNode = this.head;
52119

53-
for (int i = 1; i < index; i++)
120+
for (int i = 0; i < index; i++)
54121
prevNode = prevNode.next;
55122

56-
if (index != this.length)
57-
newNode.next = prevNode.next;
58-
123+
if (index != this.length) newNode.next = prevNode.next;
59124
prevNode.next = newNode;
60-
this.length++;
61125

126+
this.length++;
62127
return newNode;
63128
}
64129

65130
/**
66131
* Deletes the node at the specified index.
67132
*
68133
* <p>
69-
* O(n) complexity
134+
* O(n) complexity.
135+
*
70136
* @param index
71-
* @return
72-
*/
137+
* @return The deleted node.
138+
*/
73139
public LinkedListNode delete(int index) {
74-
if (index < 0 || index >= this.length || this.length == 0) return null;
140+
if (index < 0 || index >= this.length) throw new IndexOutOfBoundsException();
75141

76142
LinkedListNode prevNode = this.head;
77143

78-
for (int i = 1; i < index; i++)
144+
for (int i = 0; i < index; i++)
79145
prevNode = prevNode.next;
80146

81147
LinkedListNode node = prevNode.next;
82-
prevNode.next = node.next;
148+
if (index != this.length) prevNode.next = node.next;
83149

150+
this.length--;
84151
return node;
85152
}
153+
154+
/**
155+
* Exports this list to an array.
156+
*
157+
* @return
158+
*/
159+
public byte[] toArray() {
160+
byte[] array = new byte[this.length];
161+
LinkedListNode node = this.head;
162+
163+
for (int i = 0; i < this.length; i++) {
164+
node = node.next;
165+
array[i] = node.data;
166+
}
167+
168+
return array;
169+
}
170+
171+
public static void main(String[] args) {
172+
LinkedList list = new LinkedList();
173+
174+
for (byte i = 0; i < 10; i++)
175+
list.insert(i, list.getLength());
176+
177+
list.delete(list.getLength());
178+
179+
byte[] array = list.toArray();
180+
181+
for (byte b: array) {
182+
System.out.println(b);
183+
}
184+
}
86185
}

0 commit comments

Comments
 (0)