forked from anthonynsimon/java-ds-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedList.java
217 lines (177 loc) · 5.6 KB
/
LinkedList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.anthonynsimon.datastructures;
import com.anthonynsimon.datastructures.util.SinglyNode;
public class LinkedList<E> {
protected int size;
protected SinglyNode<E> head;
public LinkedList() {
this.size = 0;
this.head = null;
}
public LinkedList(E item) {
append(item);
}
public void build(E[] items) {
clear();
for (int i = 0; i < items.length; i++) {
append(items[i]);
}
}
public int size() {
return this.size;
}
public E peek() {
if (size() == 0) {
return null;
}
return this.head.getData();
}
public void append(E item) {
SinglyNode<E> newNode = new SinglyNode<E>(item);
// If list is empty, setup initial nodes
if (size() == 0) {
this.head = newNode;
}
// If the list is not empty, add as last element
else {
SinglyNode<E> lastNode = getNodeAtIndex(size() - 1);
lastNode.setNext(newNode);
}
this.size++;
}
public E get(int index) throws IndexOutOfBoundsException {
if (isOutOfRange(index)) {
throw new IndexOutOfBoundsException();
}
SinglyNode<E> selectedNode = getNodeAtIndex(index);
return selectedNode.getData();
}
public void set(int index, E item) throws IndexOutOfBoundsException {
if (isOutOfRange(index)) {
throw new IndexOutOfBoundsException();
}
SinglyNode<E> selectedNode = getNodeAtIndex(index);
selectedNode.setData(item);
}
public void insert(int index, E item) throws IndexOutOfBoundsException {
if (isOutOfRange(index)) {
throw new IndexOutOfBoundsException();
}
SinglyNode<E> newNode = new SinglyNode<E>(item);
// If it's first node, set as new head
if (index == 0) {
newNode.setNext(this.head);
this.head = newNode;
}
// If not, find previous node to selected index and reconnect after it
else {
SinglyNode<E> previousNode = getNodeAtIndex(index - 1);
newNode.setNext(previousNode.getNext());
previousNode.setNext(newNode);
}
this.size++;
}
public void remove(int index) throws IndexOutOfBoundsException {
if (isOutOfRange(index)) {
throw new IndexOutOfBoundsException();
}
// If removing head node, simply set it's next one
if (index == 0) {
this.head = this.head.getNext();
} else {
// Find previous node
SinglyNode<E> previousNode = getNodeAtIndex(index - 1);
// If trying to remove last set it's previous node next connection to null
if (index == size() - 1) {
previousNode.setNext(null);
}
// If not, interconnect previous and next
else {
SinglyNode<E> selectedNode = previousNode.getNext();
previousNode.setNext(selectedNode.getNext());
}
}
this.size--;
}
public boolean contains(E item) {
SinglyNode<E> current = this.head;
while (current != null) {
if (current.getData() == item) {
return true;
}
current = current.getNext();
}
return false;
}
public E pop() {
if (size() == 0) {
return null;
}
// Temporary variable to hold the data to be popped
E temp;
// If only one item is left, pop head
if (size() == 1) {
temp = this.head.getData();
this.head = null;
}
// If not, then save the last node's data
// and set the previous node's next connection to null
else {
SinglyNode<E> previous = getNodeAtIndex(size() - 2);
SinglyNode<E> last = previous.getNext();
temp = last.getData();
previous.setNext(null);
}
this.size--;
return temp;
}
public E getLast() {
SinglyNode<E> selectedNode = getNodeAtIndex(size() - 1);
return selectedNode != null ? selectedNode.getData() : null;
}
public void clear() {
this.head = null;
this.size = 0;
}
public E[] toArray() {
// It is safe to suppress unchecked exception because the array we're creating
// is of the same type as the one passed.
@SuppressWarnings("unchecked")
E[] result = (E[]) new Object[size()];
SinglyNode<E> current = this.head;
for (int i = 0; i < size(); i++) {
result[i] = current.getData();
current = current.getNext();
}
return result;
}
public String toString() {
return toString(" ");
}
public String toString(String glue) {
StringBuilder sb = new StringBuilder();
SinglyNode<E> current = this.head;
for (int i = 0; i < size(); i++) {
sb.append(current.getData());
if (i != size() - 1) {
sb.append(glue);
}
current = current.getNext();
}
return sb.toString();
}
private boolean isOutOfRange(int index) {
return index < 0 || size() <= index;
}
private SinglyNode<E> getNodeAtIndex(int index) {
int i = 0;
SinglyNode<E> current = this.head;
while (current != null) {
if (i == index) {
return current;
}
current = current.getNext();
i++;
}
return null;
}
}