-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
304 lines (276 loc) · 7.5 KB
/
LinkedList.java
File metadata and controls
304 lines (276 loc) · 7.5 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import java.io.Serializable;
public class LinkedList<T> implements Serializable {
private Node first;
private Node last;
private int size;
public LinkedList() {
first = null;
last = null;
size = 0;
}
// Adding
public void addToFront(T data) {
Node newNode = new Node(data);
// If the list isn't empty
if(getSize() != 0) {
Node currentFirst = getFirst();
// New node next (currently null) = first
newNode.setNext(currentFirst);
// First previous (currently null) = new node, which is our new first
currentFirst.setPrevious(newNode);
// If the list is empty
} else {
// Set last (currently null) to the new node since it's also the back
setLast(newNode);
}
// Regardless if first is null or previous, set first to new node
setFirst(newNode);
setSize(getSize() + 1);
}
public void addToBack(T data) {
// If the list is empty, add to the front
if(getSize() == 0) {
addToFront(data);
return;
}
// If the list isn't empty
Node newNode = new Node(data);
Node currentLast = getLast();
// Last next (currently null) = new node, which is our new last
currentLast.setNext(newNode);
// New node previous (currently null) = old last
newNode.setPrevious(currentLast);
setLast(newNode);
setSize(getSize() + 1);
}
public void add(T data, int index) {
// If the index is the first index, add to the front
if(index == 0) {
addToFront(data);
return;
// If the index is the last index, add to the back
} else if(index == getSize() - 1) {
addToBack(data);
return;
}
Node newNode = new Node(data);
Node current = null;
// If the index is closer to the front
if(startFromFrontOrBack(index) == getFirst()) {
current = getFirst();
// Traverse list until we find the node AFTER where we need to insert
for(int i = 0; i < index - 1; i++) {
current = current.getNext();
}
// If the index is closer to the back
} else {
current = getLast();
/* Traverse list backwards until we find the node BEFORE where we
need to insert */
for(int i = getSize(); i > index; i--) {
current = current.getPrevious();
}
}
Node next = current.getNext();
// New node next (currently null) = current position next
newNode.setNext(next);
// New node previous (currently null) = current position
newNode.setPrevious(current);
/* If there is a next node, which there should be or we'd be adding to
back */
if(next != null) {
// Next node previous (currently "current") = new node
next.setPrevious(newNode);
}
// Current next (currently "next") = new node
current.setNext(newNode);
setSize(getSize() + 1);
}
// Removing
public void removeFromFront() {
// If the list is empty, nothing to delete
if(getSize() == 0) {
return;
}
Node currentFirst = getFirst();
// New first may be null if we're deleting the last node
Node newFirst = currentFirst.getNext();
// First = next, may be null
setFirst(newFirst);
/* If there is a previous, which there shouldn't be since we're
deleting the front? */
if(currentFirst.getPrevious() != null) {
// Remove the connection to previous
currentFirst.setPrevious(null);
}
// If we're deleting the last node
if(newFirst == null) {
setLast(null);
// If there's only one node
} else if(newFirst.getNext() == null) {
setLast(newFirst);
}
setSize(getSize() - 1);
}
public void removeFromBack() {
// If the list is empty, nothing to delete
if(getSize() == 0) {
return;
// If the list has 1 node (being both front and back), remove from front
} else if(getSize() == 1) {
removeFromFront();
}
Node currentLast = getLast();
// New last = second-last
Node newLast = currentLast.getPrevious();
// New last next = null
newLast.setNext(null);
setLast(newLast);
setSize(getSize() - 1);
}
public void remove(T objectToRemove) {
// If the list is empty, nothing to delete
if(getSize() == 0) {
return;
}
int index = indexOf(objectToRemove);
// If the index is the front, remove from front
if(index == 0) {
removeFromFront();
return;
}
// If the index is the back, remove from back
if(index == getSize() - 1) {
removeFromBack();
return;
}
Node nodeToRemove = null;
// If index is closer to the front
if(startFromFrontOrBack(index) == getFirst()) {
nodeToRemove = getFirst();
// Traverse the list until we get the node to be deleted
for(int i = 0; i < index; i++) {
nodeToRemove = nodeToRemove.getNext();
}
// If index is closer to the back
} else {
nodeToRemove = getLast();
// Traverse the list backwards until we get the node to be deleted
for(int i = getSize(); i > index + 1; i--) {
nodeToRemove = nodeToRemove.getPrevious();
}
}
Node previous = nodeToRemove.getPrevious();
Node next = nodeToRemove.getNext();
/* Previous node next = next of node to be removed, skips over the node
to remove */
previous.setNext(next);
/* If there is a previous to next, which there always should be because
that's the node to remove? */
if(next.getPrevious() != null) {
/* Next node previous = previous of node to be removed, skips over
the node to remove */
next.setPrevious(previous);
}
setSize(getSize() - 1);
}
// Moving
// To move, simply delete the node and re-add it to the desired position
public void move(T data, int index) {
// If the node is already at the desired position
if(indexOf(data) == index) {
return;
}
remove(data);
add(data, index);
}
private Node startFromFrontOrBack(int desiredIndex) {
/* If the desired index is closer to 0 (first) or the size of the index
(last) */
if(Math.abs(desiredIndex) < Math.abs(desiredIndex - getSize())) {
return getFirst();
} else {
return getLast();
}
}
// Searching
/* Traverse through the list until you find the object and return the index
found */
public int indexOf(T objectToFind) {
Node current = getFirst();
for(int i = 0; i < getSize(); i++) {
if(current.getData() == objectToFind) {
return i;
}
current = current.getNext();
}
// If the object isn't found, return -1
return -1;
}
// Traverse through the list until you find the object and return the node
public Node nodeOfObject(T objectToFind) {
Node current = getFirst();
for(int i = 0; i < getSize(); i++) {
if(current.getData() == objectToFind) {
return current;
}
current = current.getNext();
}
// If the object isn't found, return null
return null;
}
// Traverse through the list until you get to the index and return the node
public Node nodeOfIndex(int desiredIndex) {
Node currentNode = getFirst();
for(int currentIndex = 0; currentIndex < getSize(); currentIndex++) {
if(currentIndex == desiredIndex) {
return currentNode;
}
currentNode = currentNode.getNext();
}
return null;
}
public Node getFirst() {
return first;
}
private void setFirst(Node first) {
this.first = first;
}
public Node getLast() {
return last;
}
private void setLast(Node last) {
this.last = last;
}
public int getSize() {
return size;
}
private void setSize(int size) {
this.size = size;
}
public class Node implements Serializable {
private final T data;
private Node previous;
private Node next;
public Node(T data) {
this.data = data;
previous = null;
next = null;
}
public T getData() {
return data;
}
public Node getPrevious() {
return previous;
}
protected void setPrevious(Node previous) {
this.previous = previous;
}
public Node getNext() {
return next;
}
protected void setNext(Node next) {
this.next = next;
}
}
}