-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
251 lines (226 loc) · 7.03 KB
/
DoublyLinkedList.java
File metadata and controls
251 lines (226 loc) · 7.03 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
/**
* A generic doubly-linked list that maintains sorted order on insert.
*
* @param <T> the element type (must implement Comparable)
*/
public class DoublyLinkedList<T extends Comparable<T>> {
private NodeType<T> head;
private NodeType<T> tail;
private int size;
/**
* Construct an empty DoublyLinkedList.
*/
public DoublyLinkedList() {
head = null;
tail = null;
size = 0;
}
/**
* Delete the first occurrence of item from the list.
*
* @param item element to remove
* @throws IllegalArgumentException if the list is empty or the item is not
* found
*/
public void deleteItem(T item) {
if (head == null) {
throw new IllegalArgumentException("You cannot delete from an empty list");
}
NodeType<T> curr = head;
//Iterate
while (curr != null && curr.getInfo().compareTo(item) != 0) {
curr = curr.getNext();
}
//Unfound Case
if (curr == null) {
throw new IllegalArgumentException("The item is not present in the list");
}
//Found Case
if (curr == head) {
head = head.getNext();
if (head != null) {
head.setBack(null);
} else {
tail = null;
}
size--;
} else if (curr == tail) {
tail = tail.getBack();
if (tail != null) {
tail.setNext(null);
} else {
head = null;
}
size--;
} else {
curr.getBack().setNext(curr.getNext());
curr.getNext().setBack(curr.getBack());
size--;
}
}
/**
* Insert an item into the list keeping the list sorted in ascending order.
* Duplicate values are not allowed.
*
* @param item element to insert
* @throws IllegalArgumentException if the item already exists
*/
public void insertItem(T item) {
NodeType<T> newNode = new NodeType<>();
newNode.setInfo(item);
NodeType<T> curr = head;
if (head == null) {
head = newNode;
tail = newNode;
size++;
} else {
// Iterate
while (curr != null && curr.getInfo().compareTo(item) < 0) {
curr = curr.getNext();
}
// Duplicate check
if (curr != null && curr.getInfo().compareTo(item) == 0) {
throw new IllegalArgumentException("Item already exists");
}
// Insert at end
if (curr == null) {
tail.setNext(newNode);
newNode.setBack(tail);
tail = newNode;
size++;
} else {
// Insert before curr
NodeType<T> prev = curr.getBack();
newNode.setNext(curr);
curr.setBack(newNode);
newNode.setBack(prev);
if (prev != null) {
prev.setNext(newNode);
} else {
// inserting at head
head = newNode;
}
size++;
}
}
}
/**
* Return the current number of elements in the list.
*
* @return size of the list
*/
public int length() {
return size;
}
/**
* Print the list contents from head to tail to standard output. If the list
* is empty this method prints nothing.
*/
public void print() {
if (head == null) {
// Print an empty line so callers that print a prefix (e.g. "The original list: ")
// always get a newline even when the list is empty.
System.out.println();
return;
}
NodeType<T> curr = head;
while (curr != null) {
System.out.print(curr.getInfo() + " ");
curr = curr.getNext();
}
System.out.println();
}
/**
* Print the list contents from tail to head to standard output. If the list
* is empty this method prints nothing.
*/
public void printReverse() {
// Always print the heading so callers (like bCalled) will see the
// "The reverse list: " label even when the list is empty.
System.out.print("The reverse list: ");
if (head == null) {
System.out.println();
return;
}
NodeType<T> curr = tail;
while (curr != null) {
System.out.print(curr.getInfo() + " ");
curr = curr.getBack();
}
System.out.println();
}
/**
* Delete all items x such that lower <= x <= upper.
*
* @param lower lower bound (inclusive)
* @param upper upper bound (inclusive)
* @throws IllegalArgumentException if the list is empty
*/
public void deleteSubsection(T lower, T upper) {
NodeType<T> curr = head;
while (curr != null) {
NodeType<T> next = curr.getNext();
if (curr.getInfo().compareTo(lower) >= 0 && curr.getInfo().compareTo(upper) <= 0) {
// deleteItem will update head/tail/size correctly
deleteItem(curr.getInfo());
}
curr = next;
}
}
/**
* Reverse the list in-place (swap next/back pointers and swap head/tail).
* This is a linear-time operation.
*/
public void reverseList() {
// empty or single-element list: nothing to do
if (head == null || head.getNext() == null) {
return;
}
NodeType<T> curr = head;
while (curr != null) {
NodeType<T> temp = curr.getNext();
curr.setNext(curr.getBack());
curr.setBack(temp);
curr = temp;
}
NodeType<T> temp = head;
head = tail;
tail = temp;
}
/**
* Swap every pair of adjacent nodes. If the list
* has odd length the final node remains in place.
*/
public void swapAlternate() {
// empty or single-element list: nothing to do
if (head == null || head.getNext() == null) {
return;
}
NodeType<T> curr = head;
while (curr != null && curr.getNext() != null) {
NodeType<T> first = curr;
NodeType<T> second = curr.getNext();
NodeType<T> prev = first.getBack();
NodeType<T> next = second.getNext();
// Link prev to second
if (prev != null) {
prev.setNext(second);
} else {
head = second;
}
// Link next back to first
if (next != null) {
next.setBack(first);
} else {
tail = first;
}
// Swap first and second
second.setBack(prev);
second.setNext(first);
first.setBack(second);
first.setNext(next);
// advance to the node after the swapped pair
curr = first.getNext();
}
}
}