-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path02-DoublyLinkedList.java
309 lines (249 loc) · 6.67 KB
/
02-DoublyLinkedList.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
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
305
306
307
308
309
package basic.data.structures;
import java.util.Iterator;
/**
* Doubly Linked List.
* Each element has a link to the next element and the previous element.
*
* O(1) access, insertion and deletion at the beginning of the list.
* O(1) access, insertion and deletion at the ending of the list.
* O(size) access, insertion and deletion in the middle of the list.
*
* @param <T> Type of the objects stored in the Linked List
*/
public class DoublyLinkedList<T> implements Iterable<T> {
private class Node {
T value;
Node next;
Node previous;
Node () {
value = null;
next = null;
previous = null;
}
Node (T v, Node n, Node p) {
value = v;
next = n;
previous = p;
}
}
private Node first;
private Node last;
private int size;
/**
* Create an empty doubly linked list.
*/
public DoublyLinkedList() {
first = null;
last = null;
size = 0;
}
/**
* Insert the object at the end of the doubly linked list.
*
* @param value The object ot be inserted.
*/
public void addToEnd(T value) {
size++;
if (last == null) {
first = new Node(value, null, null);
last = first;
return;
}
last.next = new Node(value, null, last);
last = last.next;
}
/**
* Insert the object at the start of the doubly linked list.
*
* @param value The object ot be inserted.
*/
public void addToBeginning(T value) {
size++;
if (first == null) {
first = new Node(value, null, null);
last = first;
return;
}
first = new Node(value, first, null);
}
/**
* Insert the object at a given position in the doubly linked list.
*
* @param value The object ot be inserted.
* @param position The position of the insertion.
*/
public void insertAt(T value, int position) {
if (position < 0) {
throw new IllegalArgumentException("Can't add element at negative position");
}
if (position >= size) {
addToEnd(value);
return;
}
if (position == 0) {
addToBeginning(value);
return;
}
size++;
Node temp = goToPosition(position - 1);
temp.next = new Node(value, temp.next, temp);
temp.next.next.previous = temp.next;
}
/**
* Erase the first element of the doubly linked list.
*/
public void eraseBeginning() {
if (size == 0) {
throw new IllegalStateException("Can't delete form an empty LinkedList");
}
size--;
first = first.next;
if (first != null) {
first.previous = null;
}
}
/**
* Erase the last element of the doubly linked list.
*/
public void eraseEnd() {
if (size == 0) {
throw new IllegalStateException("Can't delete form an empty LinkedList");
}
size--;
last = last.previous;
if (last != null) {
last.next = null;
}
}
/**
* Erase the element at a given position in the doubly linked list.
*
* @param position The position of the object.
*/
public void eraseAt(int position) {
if (position < 0 || position >= size) {
throw new IllegalArgumentException("Position is out of bounds");
}
if (position == 0) {
eraseBeginning();
return;
}
if (position == size - 1) {
eraseEnd();
return;
}
size--;
Node temp = goToPosition(position - 1);
temp.next = temp.next.next;
temp.next.previous = temp;
}
/**
* Return the last element of the doubly linked list.
*
* @return Reference to the object.
*/
public T getBeginning() {
if (size == 0) {
return null;
}
return first.value;
}
/**
* Return the first element of the doubly linked list.
*
* @return Reference to the object.
*/
public T getEnd() {
if (size == 0) {
return null;
}
return last.value;
}
/**
* Return the element at a given position in the doubly linked list.
*
* @param position The position of the object.
* @return Reference to the object.
*/
public T get(int position) {
if (position < 0 || position >= size) {
throw new IllegalArgumentException("Position is out of bounds");
}
return goToPosition(position).value;
}
/**
* Reverse the elements of the linked list.
* O(size) complexity.
*/
public void reverse() {
Node forward = first;
Node backward = last;
int cnt = 0;
while (cnt < size / 2) {
T temp = forward.value;
forward.value = backward.value;
backward.value = temp;
forward = forward.next;
backward = backward.previous;
cnt++;
}
}
/**
* @return Current number of elements in the linked list.
*/
public int size() {
return size;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
this.forEach(value -> sb.append(value.toString()).append(", "));
if (size > 0) {
sb.delete(sb.length() - 2, sb.length());
}
sb.append("]");
return sb.toString();
}
@Override
public Iterator<T> iterator() {
return new LinkedListIterator();
}
private Node goToPosition(int position) {
if (position < size - position) {
int index = 0;
Node temp = first;
while (index < position) {
index++;
temp = temp.next;
}
return temp;
}
int index = size - 1;
Node temp = last;
while (index > position) {
index--;
temp = temp.previous;
}
return temp;
}
private class LinkedListIterator implements Iterator<T> {
private int index = 0;
private Node current = first;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public T next() {
T value = current.value;
index++;
current = current.next;
return value;
}
@Override
public void remove() {
eraseAt(index);
}
}
}