-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
253 lines (215 loc) · 7.05 KB
/
DoublyLinkedList.java
File metadata and controls
253 lines (215 loc) · 7.05 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
import java.util.Iterator;
import javax.management.RuntimeErrorException;
public class DoublyLinkedList <T> implements Iterable <T> {
private int size = 0;
private Node <T> head = null;
private Node <T> tail = null;
private class Node <T> {
T data;
Node <T> prev, next;
public Node(T data, Node <T> prev, Node <T> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
@Override public String toString() {
return data.toString();
}
}
// Empty this linked list, O(n)
public void clear() {
Node <T> trav = head;
while (trav != null) {
Node <T> next = trav.next;
trav.prev = trav.next = null;
trav.data = null;
trav = next;
}
head = tail = trav = null;
size = 0;
}
// Return the size of this linked list
public int size() {
return size;
}
// Is this linked list empty?
public boolean isEmpty() {
return size() == 0;
}
// Add an element to the tail of the linked list, O(1)
public void add(T elem) {
addLast(elem);
}
// Add an element to the beginning of this linked list, O(1)
public void addFirst(T elem) {
// The linked list is empty
if (isEmpty()) {
head = tail = new Node <T> (elem, null, null);
}
else {
head.prev = new Node <T> (elem, null, head);
head = head.prev;
}
size++;
}
// Add a node to the tail of the linked list, O(1)
public void addLast(T elem) {
// The linked list is empty
if (isEmpty()) {
head = tail = new Node <T> (elem, null, null);
}
else {
tail.next = new Node <T> (elem, tail, null);
tail = tail.next;
}
size++;
}
// Check the value of the first node if it exists, O(1)
public T peekFirst() {
if (isEmpty()) throw new RuntimeException("Empty list");
return head.data;
}
// Check the value of the last node if it exists, O(1)
public T peekLast() {
if (isEmpty()) throw new RuntimeException("Empty list");
return tail.data;
}
// Remove the first value at the head of the linked list, O(1)
public T removeFirst() {
// Can't remove data from an empty lsit -_-
if (isEmpty()) throw new RuntimeException("Empty list");
// Extract the data at the head and move
// the head pointer forwards one node
T data = head.data;
head = head.next;
--size;
// If the list is empty set the tail to null as well
if (isEmpty()) tail = null;
// Do a memory clean of the previous node
else head.prev = null;
// Return the data that was at the first node we just removed
return data;
}
// Remove the last value at the tail of the linked list, O(1)
public T removeLast() {
// Can't remove data from an empty list
if (isEmpty()) throw new RuntimeException("Empty list");
// Extract the data at the tail and move
// the tail pointer backwards one node
T data = tail.data;
tail = tail.prev;
--size;
// If the list is now empty set the head to null
if (isEmpty()) head = null;
// Do a memory clean of the node that was just removed
else tail.next = null;
// Return the data that was in the last node we just removed
return data;
}
// Removed an arbitrary node from the linked list, O(1)
private T remove(Node <T> node) {
// If the node to remove is somewhere either at the
// head or the tail handle those independently
if (node.prev == null) return removeFirst();
if (node.next == null) return removeLast();
// Make the pointers of adjacent nodes skip over 'node'
node.next.prev = node.prev;
node.prev.next = node.next;
// Temporary store the data we want to return
T data = node.data;
// Memory cleanup
node.data = null;
node = node.prev = node.next = null;
--size;
// Return the data at the node we just removed
return data;
}
// Find the index of a particular value in the linked list, O(n)
public int indexOf(Object obj) {
int index = 0;
Node <T> trav = head;
// Support searching for null
if (obj == null) {
for (trav = head; trav != null; trav = trav.next, index++) {
if (trav.data == null)
return index;
}
}
// Search for non null object
else {
for (trav = head; trav != null; trav = trav.next, index++) {
if (obj.equals(trav.data))
return index;
}
}
return -1;
}
// Remove a node at a particular index, O(n)
public T removeAt(int index) {
// Make sure the index provided is valid -_-
if (index < 0 || index >= size) throw new IllegalArgumentException();
int i;
Node <T> trav;
// Search from the front of the list
if (index < size/2) {
for (i = 0, trav = head; i != index; i++) {
trav = trav.next;
}
// Search from the back of the list
} else
for (i = size-1, trav = tail; i != index; i--)
trav = trav.prev;
return remove(trav);
}
// Remove a particular value in the linked list, O(n)
public boolean remove(Object obj) {
Node <T> trav = head;
// Support searching for null
if (obj == null) {
for (trav = head; trav != null; trav = trav.next) {
if (trav.data == null) {
remove(trav);
return true;
}
}
}
// Search for non null object
else {
for(trav = head; trav != null; trav = trav.next) {
if (obj.equals(trav.data)) {
remove(trav);
return true;
}
}
}
return false;
}
// Check is a value is contained within the linked list
public boolean contains(Object obj) {
return indexOf(obj) != -1;
}
@Override public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[ ");
Node <T> trav = head;
while (trav != null) {
sb.append(trav.data + ", ");
trav = trav.next;
}
sb.append(" ]");
return sb.toString();
}
@Override public java.util.Iterator <T> iterator () {
return new java.util.Iterator <T>() {
private Node <T> trav = head;
@Override public boolean hasNext() {
return trav != null;
}
@Override public T next() {
T data = trav.data;
trav = trav.next;
return data;
}
};
}
}