-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLList.java
More file actions
218 lines (184 loc) · 3.64 KB
/
Copy pathDLList.java
File metadata and controls
218 lines (184 loc) · 3.64 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
// YOU SHOULD NOT MODIFY THIS FILE AT ALL.
// (This version will be used by the autograder; not yours!)
import java.util.AbstractSequentialList;
import java.util.ListIterator;
/**
* An implementation of the List interface as a doubly-linked circular list. A
* dummy node is used to simplify the code.
*
* @author morin
*
* @param <T>
* the type of elements stored in the list
*/
public class DLList<T> extends AbstractSequentialList<T> {
class Node {
T x;
Node prev, next;
}
/**
* Number of nodes in the list
*/
int n;
/**
* The dummy node. We use the convention that dummy.next = first and
* dummy.prev = last
*/
protected Node dummy;
public DLList() {
dummy = new Node();
dummy.next = dummy;
dummy.prev = dummy;
n = 0;
}
/**
* Add a new node containing x before the node p
*
* @param w
* the node to insert the new node before
* @param x
* the value to store in the new node
* @return the newly created and inserted node
*/
protected Node addBefore(Node w, T x) {
Node u = new Node();
u.x = x;
u.prev = w.prev;
u.next = w;
u.next.prev = u;
u.prev.next = u;
n++;
return u;
}
/**
* Remove the node p from the list
*
* @param w
* the node to remove
*/
protected void remove(Node w) {
w.prev.next = w.next;
w.next.prev = w.prev;
n--;
}
/**
* Get the i'th node in the list
*
* @param i
* the index of the node to get
* @return the node with index i
*/
protected Node getNode(int i) {
Node p = null;
if (i < n / 2) {
p = dummy.next;
for (int j = 0; j < i; j++)
p = p.next;
} else {
p = dummy;
for (int j = n; j > i; j--)
p = p.prev;
}
return p;
}
public int size() {
return n;
}
public boolean add(T x) {
addBefore(dummy, x);
return true;
}
public T remove(int i) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
Node w = getNode(i);
remove(w);
return w.x;
}
public void add(int i, T x) {
if (i < 0 || i > n) throw new IndexOutOfBoundsException();
addBefore(getNode(i), x);
}
public T get(int i) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
return getNode(i).x;
}
public T set(int i, T x) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
Node u = getNode(i);
T y = u.x;
u.x = x;
return y;
}
public void clear() {
dummy.next = dummy;
dummy.prev = dummy;
n = 0;
}
public ListIterator<T> listIterator(int i) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
return new Iterator(this, i);
}
public ListIterator<T> listIterator() {
return new Iterator(this, -1);
}
class Iterator implements ListIterator<T> {
/**
* The list we are iterating over
*/
DLList<T> l;
/**
* The node whose value is returned by next()
*/
Node p;
/**
* The last node whose value was returned by next() or previous()
*/
Node last;
/**
* The index of p
*/
int i;
Iterator(DLList<T> il, int ii) {
l = il;
i = ii;
p = l.getNode(i); // if i == -1, p = dummy
}
public boolean hasNext() {
return p != dummy;
}
public T next() {
T x = p.x;
last = p;
p = p.next;
i++;
return x;
}
public int nextIndex() {
return i;
}
public boolean hasPrevious() {
return p.prev != dummy;
}
public T previous() {
p = p.prev;
last = p;
i--;
return p.x;
}
public int previousIndex() {
return i - 1;
}
public void add(T x) {
DLList.this.addBefore(p, x);
}
public void set(T x) {
last.x = x;
}
public void remove() {
if (p == last) {
p = p.next;
}
DLList.this.remove(last);
}
}
}