-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedLinkedSet.java
More file actions
292 lines (239 loc) · 8.15 KB
/
SortedLinkedSet.java
File metadata and controls
292 lines (239 loc) · 8.15 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
/* @author Pepe Gallardo, Data Structures, Grado en Informática. UMA.
*
* Sets implemented using a sorted linked structure
*/
package set;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.StringJoiner;
public class SortedLinkedSet<T extends Comparable<? super T>> implements SortedSet<T> {
// A node in the linked structure
static private class Node<E> {
E elem;
Node<E> next;
Node(E x, Node<E> node) {
elem = x;
next = node;
}
}
// INVARIANTS: Nodes for elements included in the set are kept in a sorted
// linked structure (sorted in ascending order with respect to
// their elements). In addition, there should be no repetitions
// of elements in the linked structure.
private Node<T> first; // Reference to first (with the smallest element) node
private int size; // Number of elements in this set
// Constructs an empty set
public SortedLinkedSet() {
first = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
private class Finder {
boolean found;
Node<T> previous, current;
Finder(T elem) {
// todo
// An invocation of this constructor should
// search for elem in this class sorted linked structure.
// Attribute found should be set to true if elem was
// found or false otherwise.
// At the end of the search, current
// should be a reference to the node storing elem
// and previous should be a reference to node
// before current (or null if elem was found at first node).
if (isEmpty()) {
found = false;
previous = null;
current = null;
} else {
current = first;
previous = null;
while (!found && current != null) {
if (elem.compareTo(current.elem) == 0) {
found = true;
} else if (elem.compareTo(current.elem) < 0) {
break;
} else {
previous = current;
current = current.next;
}
}
}
}
}
public void insert(T elem) {
// todo
// Implement insert by using Finder.
// insert should add a new node for elem
// to this class sorted linked structure
// if elem is not yet in this set.
Finder f = new Finder(elem);
if (!f.found) {
if (f.previous == null) {
f.previous = new Node<T>(elem, f.current);
first = f.previous;
} else {
f.previous.next = new Node<T>(elem, f.current);
}
size++;
}
}
public boolean isElem(T elem) {
// todo
// Implement isElem by using Finder.
// isElem should return true is elem
// is in this class sorted linked structure
// or false otherwise.
return new Finder(elem).found;
}
public void delete(T elem) {
// todo
// Implement delete by using Finder.
// delete should remove the node containing elem
// from this class sorted linked structure
// if elem is in this set.
Finder f = new Finder(elem);
if (f.found) {
if (f.previous == null) {
first = null;
} else {
f.previous.next = f.current.next;
}
size--;
}
}
public String toString() {
String className = getClass().getSimpleName();
StringJoiner sj = new StringJoiner(", ", className + "(", ")");
for (Node<T> node = first; node != null; node = node.next)
sj.add(node.elem.toString());
return sj.toString();
}
/**
* Iterator over elements in this set.
* Note that {@code remove} method is not supported. Note also
* that linked structure should not be modified during iteration as
* iterator state may become inconsistent.
*
* @see java.lang.Iterable#iterator()
*/
public Iterator<T> iterator() {
return new LinkedSetIterator();
}
private class LinkedSetIterator implements Iterator<T> {
Node<T> current; // A reference to node with value that will be iterated next
public LinkedSetIterator() {
// todo
// Initialize iterator by making current a reference to first node
current = first;
}
public boolean hasNext() {
// todo
// Check if all elements have already been returned by this iterator
return current != null;
}
public T next() {
// todo
// Check if there are still more elements to be returned (raise
// NoSuchElementException otherwise), return next element and
// advance iterator to next node for next iteration.
if (current.next == null) {
throw new NoSuchElementException("");
}
T element = current.elem;
current = current.next;
return element;
}
}
// private constructor for building a SortedLinkedSet
// by providing a reference to first node and size
private SortedLinkedSet(Node<T> first, int size) {
this.first = first;
this.size = size;
}
// a buffer can be used to construct a SortedLinkedSet
// efficiently in an incremental way by appending elements
// in ascending order
private static class SortedLinkedSetBuffer<T extends Comparable<? super T>> {
Node<T> first, last; // references to first and last nodes in buffer
int size; // number of elements in buffer
// Builds an empty buffer
SortedLinkedSetBuffer() {
first = null;
last = null;
size = 0;
}
// Adds a new element at the end of buffer.
// precondition: elem should be larger than any element
// currently in buffer
void append(T elem) {
assert first == null || elem.compareTo(last.elem) > 0 : "SortedLinkedSetBuffer.append: precondition failed";
Node<T> node = new Node<>(elem, null);
if (first == null) {
first = node;
} else {
last.next = node;
}
last = node;
size++;
}
// Builds a SortedLinkedSet using this buffer.
SortedLinkedSet<T> toSortedLinkedSet() {
return new SortedLinkedSet<>(first, size);
}
}
// Copy constructor: builds a new SortedLinkedSet with the same
// elements as parameter sortedSet.
public SortedLinkedSet(SortedSet<T> sortedSet) {
// todo
// Implement this copy constructor using a SortedLinkedSetBuffer
SortedLinkedSetBuffer<T> bufferSet = new SortedLinkedSetBuffer<T>();
Iterator<T> iterator = sortedSet.iterator();
while(iterator.hasNext()) {
bufferSet.append(iterator.next());
}
this.first = bufferSet.first;
this.size = bufferSet.size;
}
public static <T extends Comparable<? super T>>
SortedLinkedSet<T> union(SortedLinkedSet<T> set1, SortedLinkedSet<T> set2) {
// todo Should compute a new SortedLinkedSet including all elements which are
// in set1 or in set2.
// Neither set1 nor set2 should be modified.
// Implement this method by using a SortedLinkedSetBuffer.
SortedLinkedSet<T> newSet = new SortedLinkedSet<T>(set1);
Iterator<T> iterator = set2.iterator();
while(iterator.hasNext()) {
newSet.insert(iterator.next());
}
return newSet;
}
public static <T extends Comparable<? super T>>
SortedLinkedSet<T> intersection(SortedLinkedSet<T> set1, SortedLinkedSet<T> set2) {
// todo Should compute a new SortedLinkedSet including only common elements in
// set1 and in set2.
// Neither set1 nor set2 should be modified.
// Implement this method by using a SortedLinkedSetBuffer.
return null;
}
public static <T extends Comparable<? super T>>
SortedLinkedSet<T> difference(SortedLinkedSet<T> set1, SortedLinkedSet<T> set2) {
// todo Should compute a new SortedLinkedSet including all elements in
// set1 which are not in set2.
// Neither set1 nor set2 should be modified.
// Implement this method by using a SortedLinkedSetBuffer.
return null;
}
public void union(SortedSet<T> sortedSet) {
// todo Should modify this set so that it becomes the union of
// this set and parameter sortedSet.
// Parameter sortedSet should not be modified.
// Should reuse current nodes in this set and should create new nodes for
// elements copied from sortedSet.
}
}