001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
|
package algs13;
import stdlib.*;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/* ***********************************************************************
* Compilation: javac LinkedList.java
* Execution: java LinkedList
*
* A list implemented with a doubly linked list. The elements are stored
* (and iterated over) in the same order that they are inserted.
*
* % java List
* This
* is
* a
* test.
*
* This
* is
* a
* test.
*
*************************************************************************/
public class LinkedList<T> implements Iterable<T> {
private int N; // number of elements on list
private final Node<T> pre; // sentinel before first item
private final Node<T> post; // sentinel after last item
private long opcount;
public LinkedList() {
pre = new Node<>();
post = new Node<>();
pre.next = post;
post.prev = pre;
}
// linked list node helper data type
private static class Node<T> {
public Node() { }
public T item;
public Node<T> next;
public Node<T> prev;
}
public boolean isEmpty() { return N == 0; }
public int size() { return N; }
// add the item to the end of the list
public void add(T item) {
Node<T> last = post.prev;
Node<T> x = new Node<>();
x.item = item;
x.next = post;
x.prev = last;
post.prev = x;
last.next = x;
N++;
opcount++;
}
public ListIterator<T> iterator() { return new LinkedListIterator(); }
// assumes no calls to add() during iteration
private class LinkedListIterator implements ListIterator<T> {
private Node<T> current = pre.next; // the node that is returned by next()
private Node<T> lastAccessed = null; // the last node to be returned by prev() or next()
// reset to null upon intervening remove() or add()
private int index = 0;
private long oc = opcount;
private void ocCheck() { if (opcount != oc) throw new ConcurrentModificationException (); }
private void ocInc() { ocCheck(); opcount++; oc++; }
public boolean hasNext() { ocCheck(); return index < N; }
public boolean hasPrevious() { ocCheck(); return index > 0; }
public int previousIndex() { ocCheck(); return index - 1; }
public int nextIndex() { ocCheck(); return index; }
public T next() {
ocCheck();
if (!hasNext()) throw new NoSuchElementException();
lastAccessed = current;
T item = current.item;
current = current.next;
index++;
return item;
}
public T previous() {
ocCheck();
if (!hasPrevious()) throw new NoSuchElementException();
current = current.prev;
lastAccessed = current;
T item = current.item;
index--;
return item;
}
// replace the item of the element that was last accessed by next() or previous()
// condition: no calls to remove() or add() after last call to next() or previous()
public void set(T item) {
ocInc();
if (lastAccessed == null) throw new Error();
lastAccessed.item = item;
}
// remove the element that was last accessed by next() or previous()
// condition: no calls to remove() or add() after last call to next() or previous()
public void remove() {
ocInc();
if (lastAccessed == null) throw new Error();
Node<T> lastPrev = lastAccessed.prev;
Node<T> lastNext = lastAccessed.next;
lastPrev.next = lastNext;
lastNext.prev = lastPrev;
N--;
if (current == lastAccessed)
current = lastNext;
else
index--;
lastAccessed = null;
}
// add element to list
public void add(T item) {
ocInc();
Node<T> first = current.prev;
Node<T> middle = new Node<>();
Node<T> last = current;
middle.item = item;
first. next = middle;
middle.next = last;
last. prev = middle;
middle.prev = first;
N++;
index++;
lastAccessed = null;
}
}
public String toString () {
StringBuilder sb = new StringBuilder ();
Iterator<T> it = this.iterator ();
if (it.hasNext ())
sb.append (it.next ());
while (it.hasNext ()) {
sb.append (" ");
sb.append (it.next ());
}
return sb.toString ();
}
public static <T> void printForward(String s, LinkedList<Integer> list, ListIterator<T> iterator) {
StdOut.println();
StdOut.println(s);
StdOut.println(list);
while (iterator.hasNext())
StdOut.format ("[%d,%d] ", iterator.nextIndex (), iterator.next ());
while (iterator.hasPrevious())
StdOut.format ("[%d,%d] ", iterator.previousIndex (), iterator.previous());
StdOut.println ();
}
public static <T> void printBackward(String s, LinkedList<Integer> list, ListIterator<T> iterator) {
StdOut.println();
StdOut.println(s);
StdOut.println(list);
while (iterator.hasPrevious())
StdOut.format ("[%d,%d] ", iterator.previousIndex (), iterator.previous());
while (iterator.hasNext())
StdOut.format ("[%d,%d] ", iterator.nextIndex (), iterator.next ());
StdOut.println ();
}
// a test client
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
list.add(93);
list.add(48);
list.add(94);
list.add(4);
list.add(48);
list.add(94);
ListIterator<Integer> iterator = list.iterator();
printForward ("original", list, iterator);
// set forwards
while (iterator.hasNext()) {
int x = iterator.next();
iterator.set(x + 1);
}
printBackward ("after set forward", list, iterator);
// set backwards
while (iterator.hasPrevious()) {
int x = iterator.previous();
iterator.set(3 * x);
}
printForward ("after set backward", list, iterator);
// remove forwards
while (iterator.hasNext()) {
int x = iterator.next();
if (x % 2 == 0) iterator.remove();
}
printBackward ("after remove forward", list, iterator);
// remove backwards
while (iterator.hasPrevious()) {
int x = iterator.previous();
if (x % 5 == 0) iterator.remove();
}
printForward ("after remove backward", list, iterator);
// add forwards
while (iterator.hasNext()) {
int x = iterator.next();
iterator.add(x + 10);
}
printBackward ("after add forward", list, iterator);
// add backwards
while (iterator.hasPrevious()) {
int x = iterator.previous();
iterator.add(x - 1);
iterator.previous();
}
printForward ("after add backward", list, iterator);
}
}
|