Repository navigation
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
308 lines (272 loc) · 10 KB
/
Copy pathDoublyLinkedList.java
File metadata and controls
308 lines (272 loc) · 10 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
305
306
307
308
/*
* Implementation of dually pointed linked list.
* Each node has data and two pointers who point at a node before and ahead of it.
* Author - Aakash "Ash" Jain.
* Contact - [email protected].
*/
package Lists;
public class DoublyLinkedList implements LinkedList {
// Node prototype.
private class Node {
// Model data. Data can be any object
// More fields or custom objects can also be added as per requirements.
private Object data;
// Pointers to the next and previous nodes.
private Node prev, next;
// Constructor.
Node(Object obj, Node prev, Node next) {
this.data = obj;
this.prev = prev;
this.next = next;
}
}
// First node in the list.
private Node head;
// Last node in the list.
private Node tail;
// Length of the list. (Total number of nodes in the list)
public int len;
// Initialize the list.
public DoublyLinkedList() {
// Not necessary, just better practice.
this.head = this.tail = new Node(null, null, null);
this.len = 0;
}
// Add first node when the list is empty. O(1)
private int addFirst(Object obj) {
try {
// Create new node whose pointers point at null and set tail and head both to it.
this.head = this.tail = new Node(obj, null, null);
this.len++;
return 0;
} catch (Exception e) {
return 1;
}
}
// Add node at the start of the list. O(1)
public int addAtStart(Object obj) {
try {
if (this.isEmpty())
return addFirst(obj);
// Create new node whose next pointer points at the head node and
// then set head itself to this newly created node.
this.head = new Node(obj, null, this.head);
// Point former head's previous pointer to new list head.
this.head.next.prev = this.head;
this.len++;
return 0;
} catch (Exception e) {
return 1;
}
}
// Add node at the end of the list. O(1)
public int addAtEnd(Object obj) {
try {
if (this.isEmpty())
return addFirst(obj);
// Create new node whose previous pointer points at current tail,
// Set current tail's next pointer to that node and
// set tail itself to this newly created node.
this.tail = this.tail.next = new Node(obj, this.tail, null);
/* If the tail is not set.
Node current = this.head;
// Iterate over the whole list. O(n) (Inefficient)
while(current.next != null)
current = current.next;
current.next = new Node(obj, current, null);
*/
this.len++;
return 0;
} catch (Exception e) {
return -1;
}
}
// Add node at a particular index. Supports negative or overflowing indexes. O(n)
public int insertAt(Object obj, int index) {
try {
// Adjust for negative index.
if (index < 0 && index * (-1) <= this.len)
index = this.len - index * (-1);
// If negative index overflows.
else if (index < 0)
return 1;
// Check to divert to O(1) implementations.
if (index == 0)
return addAtStart(obj);
else if (index >= this.len-1)
return addAtEnd(obj);
// Optimization. Check for index being closer from tail or head and then iterate from that end. O(n/2) -> O(n).
else if (index <= this.len/2 + 1) {
Node current = this.head;
// Iterate over until node just before index is reached.
for (int i = 0; i < index; i++)
current = current.next;
// Create new node whose pointers point at node before the index and node at the index,
// and then set node before's (current's) next pointer to this node.
current.next = new Node(obj, current, current.next);
// Go to node formerly at index and set its previous pointer to newly created node.
current.next.next.prev = current.next;
} else {
Node current = this.tail;
// Iterate from tail until element just before index is reached.
for (int i = this.len; i > index; i--)
current = current.prev;
// Create new node whose pointers point at node before the index and node at the index,
// and then set node before's next pointer to this node.
current.next = new Node(obj, current, current.next);
// Go to node formerly at index and set its previous pointer to newly created node.
current.next.next.prev = current.next;
}
this.len++;
return 0;
} catch (Exception e) {
return 1;
}
}
// Return node data at a specified index. Supports negative or overflowing indexes. O(n)
public Object getAt(int index) {
try {
// Adjust for negative index.
if (index < 0 && index * (-1) <= this.len)
index = this.len - index * (-1);
// If negative index overflows.
else if (index < 0)
return null;
if (index > this.len-1)
index = this.len-1;
Node current = this.head;
// Check whether index is closer from head or tail and iterate from that end. O(n/2) -> O(n)
if (index <= this.len/2 + 1) {
for (int i = 0; i < index; i++)
current = current.next;
return current.data;
} else {
for (int i = this.len-1; i > index; i--)
current = current.next;
return current.data;
}
} catch (Exception e) {
return null;
}
}
// Remove node at specified index. Does NOT support negative or overflowing indexes. O(n)
public int removeAt(int index) {
try {
// Check for invalid index
if (index < 0 || index >= this.len)
return 1;
// If there's only one element in the list.
else if (this.len == 1) {
this.head.data = null;
this.head = this.tail = new Node(null, null, null);
}
else if (index == 0) {
this.head.data = null;
this.head = this.head.next;
}
else {
Node current = this.head;
Node previous = null;
// Iterate over the list until node at index is reached.
for (int i = 0; i < index; i++) {
previous = current;
current = current.next;
}
// Nullify the data and modify pointers of previous and next
// nodes such that they point to each other.
current.data = null;
previous.next = current.next;
current.next.prev = previous;
if (index == this.len-1)
this.tail = current;
}
this.len--;
return 0;
} catch (Exception e) {
return -1;
}
}
// Delete list. O(n)
public int clear() {
try {
Node current = this.head;
// Iterate over the list while setting data fields to null.
while (current != null) {
current.data = null;
current = current.next;
}
// Set head and tail to empty node and call in Java garbage collector.
this.head = this.tail = new Node(null, null, null);
System.gc();
this.len = 0;
return 0;
} catch (Exception e) {
return 1;
}
}
// Return true if list is empty, false otherwise. O(1)
public boolean isEmpty() {
return this.head.data == null && this.tail.data == null && this.len == 0;
}
// Return length of the list. O(1)
public int getLength() {
return this.len;
}
// Stringify the linked list data. O(n)
@Override
public String toString() {
try {
if (this.isEmpty())
return "[]\n";
Node current = this.head;
String result = "[";
while (current != null) {
// Add quotes if the object is a string.
if (current.data instanceof String)
result += "\"" + current.data + "\", ";
else
result += current.data + ", ";
current = current.next;
}
// Remove 2 unnecessary characters from the last node.
return result.substring(0, result.length()-2) + "]";
} catch (Exception e) {
return null;
}
}
// Print the linked list data.
@Deprecated
public int printList() {
try {
if (this.isEmpty())
System.out.println("THE LIST IS EMPTY:\n[]\n");
else {
System.out.print("\nDATA IN LINKED LIST IS: \n[");
Node current = this.head;
while (current != null) {
System.out.println(current.data);
if (current.next != null)
System.out.println(", ");
current = current.next;
}
System.out.println("]\n");
}
return 0;
} catch (Exception e) {
return 1;
}
}
/*
@Deprecated
public void printListRev() {
Node current = this.tail;
while (current != null) {
System.out.println(current.data);
current = current.prev;
}
}
*/
// Tests.
public static void main(String[] args) {
}
}