-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedLinkedDictionary.java
More file actions
249 lines (217 loc) · 7.3 KB
/
SortedLinkedDictionary.java
File metadata and controls
249 lines (217 loc) · 7.3 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
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* A class that implements the DictionaryInterface using a sorted linked dictionary.
* The dictionary is sorted and has distinct search keys.
* Search keys and associated values are not null.
*
* @author Ryan Wei
*/
public class SortedLinkedDictionary<K extends Comparable<? super K>, V> implements DictionaryInterface<K, V> {
private Node firstNode;
private int numberOfEntries;
/**
* Default constructor for the SortedLinkedDictionary class
*/
public SortedLinkedDictionary() {
initializeDataFields();
}
/**
* Adds a new entry to this dictionary.
* If the given key already exists in the dictionary, replaces the corresponding value.
* @param key An object that serves as the key of the new entry.
* @param value An object that is the desired value of the new entry.
* @return Either null if the new entry was added to the dictionary or the value that was associated with key if that value was replaced.
*/
public V add(K key, V value) {
V result = null;
if ((key == null) || (value == null)) {
throw new IllegalArgumentException("Cannot add null to a dictionary.");
} else {
Node currentNode = firstNode;
Node nodeBefore = null;
while ((currentNode != null) && (key.compareTo(currentNode.getKey()) > 0)) {
nodeBefore = currentNode;
currentNode = currentNode.getNextNode();
}
if ((currentNode != null) && key.equals(currentNode.getKey())) {
result = currentNode.getValue();
currentNode.setValue(value);
} else {
Node newNode = new Node(key, value);
if (nodeBefore == null) {
newNode.setNextNode(firstNode);
firstNode = newNode;
} else {
newNode.setNextNode(currentNode);
nodeBefore.setNextNode(newNode);
}
numberOfEntries++;
}
}
return result;
}
/**
* Removes a specific entry from this dictionary.
* @param key An object that serves as the key of the entry to be removed.
* @return Either the value that was removed and was associated with the key or null if no such object exists.
*/
public V remove(K key) {
V result = null;
if (key != null) {
Node currentNode = firstNode;
Node nodeBefore = null;
while ((currentNode != null) && (key.compareTo(currentNode.getKey()) > 0)) {
nodeBefore = currentNode;
currentNode = currentNode.getNextNode();
}
if ((currentNode != null) && key.equals(currentNode.getKey())) {
Node nodeAfter = currentNode.getNextNode();
if (nodeBefore == null) {
firstNode = nodeAfter;
} else {
nodeBefore.setNextNode(nodeAfter);
}
result = currentNode.getValue();
numberOfEntries--;
}
}
return result;
}
/**
* Retrieves the value associated with a given key.
* @param key An object that serves as the key of the entry to be retrieved.
* @return Either the value that is associated with the key or null if no such object exists.
*/
public V getValue(K key) {
V result = null;
Node currentNode = firstNode;
while ((currentNode != null) && (key.compareTo(currentNode.getKey()) > 0)) {
currentNode = currentNode.getNextNode();
}
if ((currentNode != null) && key.equals(currentNode.getKey())) {
result = currentNode.getValue();
}
return result;
}
/**
* Sees whether a specific entry is in this dictionary.
* @param key An object that serves as the key of the desired entry.
* @return True if key is associated with an entry in the dictionary, false if not.
*/
public boolean contains(K key) {
return getValue(key) != null;
}
/**
* Creates an Iterator that traverses all search keys in this dictionary.
* @return An Iterator that provides sequential access to the search keys in the dictionary
*/
public Iterator<K> getKeyIterator() {
return new KeyIterator();
}
/**
* Creates an Iterator that traverses all values in this dictionary.
* @return An Iterator that provides sequential access to the values in this dictionary.
*/
public Iterator<V> getValueIterator() {
return new ValueIterator();
}
/**
* Sees whether this dictionary is empty.
* @return True if the dictionary is empty, false if not.
*/
public boolean isEmpty() {
return numberOfEntries == 0;
}
/**
* Gets the size of this dictionary.
* @return The number of entries in this dictionary.
*/
public int getSize() {
return numberOfEntries;
}
/**
* Removes all entries from this dictionary.
*/
public void clear() {
initializeDataFields();
}
private class KeyIterator implements Iterator<K> {
private Node nextNode;
private KeyIterator() {
nextNode = firstNode;
}
public boolean hasNext() {
return nextNode != null;
}
public K next() {
K result;
if (hasNext()) {
result = nextNode.getKey();
nextNode = nextNode.getNextNode();
} else {
throw new NoSuchElementException();
}
return result;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
private class ValueIterator implements Iterator<V> {
private Node nextNode;
private ValueIterator() {
nextNode = firstNode;
}
public boolean hasNext() {
return nextNode != null;
}
public V next() {
V result;
if (hasNext()) {
result = nextNode.getValue();
nextNode = nextNode.getNextNode();
} else {
throw new NoSuchElementException();
}
return result;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
private void initializeDataFields() {
firstNode = null;
numberOfEntries = 0;
}
private class Node {
private K key;
private V value;
private Node next;
private Node(K searchKey, V dataValue) {
key = searchKey;
value = dataValue;
next = null;
}
private Node(K searchKey, V dataValue, Node nextNode) {
key = searchKey;
value = dataValue;
next = nextNode;
}
private K getKey() {
return key;
}
private V getValue() {
return value;
}
private void setValue(V newValue) {
value = newValue;
}
private Node getNextNode() {
return next;
}
private void setNextNode(Node nextNode) {
next = nextNode;
}
}
}