-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample125.java
More file actions
293 lines (258 loc) · 7.25 KB
/
Example125.java
File metadata and controls
293 lines (258 loc) · 7.25 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
// Example 125 from page 95
//
import java.io.*;
import java.util.*;
/*
This generic interface represents a function from A to R; in C# one
would use a generic delegate instead.
In MyLinkedList method equals(Object), the cast to MyList<T> is
unchecked and may fail at runtime.
*/
interface Mapper<A,R> {
R call(A x);
}
// A generic linked-list class with elements of type T
interface MyList<T> extends Iterable<T> {
int getCount(); // Number of elements
T get(int i); // Get element at index i
void set(int i, T item); // Set element at index i
void add(T item); // Add element at end
void insert(int i, T item); // Insert element at index i
void removeAt(int i); // Remove element at index i
<U> MyList<U> map(Mapper<T,U> f); // Map f over all elements
}
class MyLinkedList<T> implements MyList<T> {
protected int size; // Number of elements in the list
protected Node<T> first, last; // Invariant: first==null iff last==null
protected static class Node<U> {
public Node<U> prev, next;
public U item;
public Node(U item) {
this.item = item;
}
public Node(U item, Node<U> prev, Node<U> next) {
this.item = item; this.prev = prev; this.next = next;
}
}
public MyLinkedList() {
first = last = null;
size = 0;
}
@SafeVarargs
public MyLinkedList(T... arr) {
this();
for (T x : arr)
add(x);
}
@Override
public int getCount() {
return size;
}
@Override
public T get(int i) {
return getNode(i).item;
}
@Override
public void set(int i, T item) {
getNode(i).item = item;
}
private Node<T> getNode(int n) {
if (n < 0 || n >= size)
throw new IndexOutOfBoundsException();
else if (n < size/2) { // Closer to front
Node<T> node = first;
for (int i=0; i<n; i++)
node = node.next;
return node;
} else { // Closer to end
Node<T> node = last;
for (int i=size-1; i>n; i--)
node = node.prev;
return node;
}
}
@Override
public void add(T item) {
insert(size, item);
}
@Override
public void insert(int i, T item) {
if (i == 0) {
if (first == null) // and thus last == null
first = last = new Node<T>(item);
else {
Node<T> tmp = new Node<T>(item, null, first);
first.prev = tmp;
first = tmp;
}
size++;
} else if (i == size) {
if (last == null) // and thus first = null
first = last = new Node<T>(item);
else {
Node<T> tmp = new Node<T>(item, last, null);
last.next = tmp;
last = tmp;
}
size++;
} else {
Node<T> node = getNode(i);
// assert node.prev != null;
Node<T> newnode = new Node<T>(item, node.prev, node);
node.prev.next = newnode;
node.prev = newnode;
size++;
}
}
@Override
public void removeAt(int i) {
Node<T> node = getNode(i);
if (node.prev == null)
first = node.next;
else
node.prev.next = node.next;
if (node.next == null)
last = node.prev;
else
node.next.prev = node.prev;
size--;
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object that) {
return equals((MyList<T>)that); // Unchecked cast
}
public boolean equals(MyList<T> that) {
if (that != null && this.size == that.getCount()) {
Node<T> thisnode = this.first;
Iterator<T> thatiter = that.iterator();
while (thisnode != null) {
if (!thatiter.hasNext())
throw new RuntimeException("Impossible: MyLinkedList<T>.equals");
// assert next() will succeed (because of the above size test)
if (!thisnode.item.equals(thatiter.next()))
return false;
thisnode = thisnode.next;
}
// assert !hasNext(); // because of the size test
return true;
} else
return false;
}
public <U> MyList<U> map(Mapper<T,U> f) {
MyLinkedList<U> res = new MyLinkedList<U>();
for (T x : this)
res.add(f.call(x));
return res;
}
public Iterator<T> iterator() {
return new MyLinkedListIterator();
}
private class MyLinkedListIterator implements Iterator<T> {
Node<T> next; // Node holding current element, or null
public MyLinkedListIterator() {
next = first;
}
public T next() {
if (next != null) {
T res = next.item;
next = next.next;
return res;
} else
throw new NoSuchElementException();
}
public boolean hasNext() {
return next != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
class SortedList<T extends Comparable<T>> extends MyLinkedList<T> {
// Sorted insertion (not an @Override)
public void insert(T x) {
Node<T> node = first;
while (node != null && x.compareTo(node.item) > 0)
node = node.next;
if (node == null) // x > all elements; insert at end
add(x);
else { // x <= node.item; insert before node
Node<T> newnode = new Node<T>(x);
if (node.prev == null) // insert as first element
first = newnode;
else
node.prev.next = newnode;
newnode.next = node;
newnode.prev = node.prev;
node.prev = newnode;
}
}
}
interface Printable {
void print(PrintWriter fs);
}
class PrintableMyLinkedList<T extends Printable>
extends MyLinkedList<T> implements Printable
{
@Override
public void print(PrintWriter fs) {
boolean firstElement = true;
for (T x : this) {
x.print(fs);
if (firstElement)
firstElement = false;
else
fs.print(", ");
}
}
}
class MyString implements Comparable<MyString> {
public final String s;
public MyString(String s) {
if (s == null)
throw new RuntimeException("null string");
else
this.s = s;
}
public int compareTo(MyString that) {
return s.compareTo(that.s);
}
}
class Example125 {
public static void main(String[] args) {
MyLinkedList<Double> dLst
= new MyLinkedList<Double>(7.0, 9.0, 13.0, 0.0);
for (double d : dLst)
System.out.print(d + " ");
System.out.println();
MyList<Integer> iLst =
dLst.map(new Mapper<Double, Integer>() {
public Integer call(Double d) {
return d < 0 ? -1 : d > 0 ? +1 : 0;
}
});
for (int i : iLst)
System.out.print(i + " ");
System.out.println();
MyList<String> sLst =
dLst.map(new Mapper<Double, String>() {
public String call(Double d) {
return "s" + d;
}
});
for (String s : sLst)
System.out.print(s + " ");
System.out.println();
// Testing SortedList<MyString>
SortedList<MyString> sortedLst = new SortedList<MyString>();
sortedLst.insert(new MyString("New York"));
sortedLst.insert(new MyString("Rome"));
sortedLst.insert(new MyString("Dublin"));
sortedLst.insert(new MyString("Riyadh"));
sortedLst.insert(new MyString("Tokyo"));
for (MyString s : sortedLst)
System.out.print(s.s + " ");
System.out.println();
}
}