forked from srdelisser/Assignment-5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearFrequencyTable.java
More file actions
137 lines (100 loc) · 3.26 KB
/
LinearFrequencyTable.java
File metadata and controls
137 lines (100 loc) · 3.26 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
import java.util.NoSuchElementException;
/** Implements the interface <code>FrequencyTable</code> using linked
* elements. The linked structure is circular and uses a dummy node.
*
* @author Marcel Turcotte (turcott@eecs.uottawa.ca)
*/
public class LinearFrequencyTable implements FrequencyTable {
// Linked elements
private static class Elem {
private String key;
private long count;
private Elem previous;
private Elem next;
private Elem(String key, Elem previous, Elem next) {
this.key = key;
this.count = 0;
this.previous = previous;
this.next = next;
}
}
private Elem head;
private int size;
/** Constructs and empty <strong>FrequencyTable</strong>.
*/
public LinearFrequencyTable() {
head = new Elem(null, null, null); // dummy node
head.previous = head; // making the dummy node circular
head.next = head; // making the dummy node circular
size = 0;
}
/** The size of the frequency table.
*
* @return the size of the frequency table
*/
public int size() {
return size;
}
/** Returns the frequency value associated with this key.
*
* @param key key whose frequency value is to be returned
* @return the frequency associated with this key
* @throws NoSuchElementException if the key is not found
*/
public long get(String key) {
throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** Creates an entry in the frequency table and initializes its
* count to zero. The keys are kept in order (according to their
* method <strong>compareTo</strong>).
*
* @param key key with which the specified value is to be associated
* @throws IllegalArgumentException if the key was alreaddy present
*/
public void init(String key) {
throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** The method updates the frequency associed with the key by one.
*
* @param key key with which the specified value is to be associated
* @throws NoSuchElementException if the key is not found
*/
public void update(String key) {
throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** Returns the list of keys in order, according to the method
* <strong>compareTo</strong> of the key objects.
*
* @return the list of keys in order
*/
public LinkedList<String> keys() {
throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** Returns an array containing the frequencies of the keys in the
* order specified by the method <strong>compareTo</strong> of
* the key objects.
*
* @return an array of frequency counts
*/
public long[] values() {
throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
}
/** Returns a <code>String</code> representations of the elements
* of the frequency table.
*
* @return the string representation
*/
public String toString() {
StringBuffer str = new StringBuffer("{");
Elem p = head.next;
while (p != head) {
str.append("{key="+p.key+", count="+p.count+"}");
if (p.next != head) {
str.append(",");
}
p = p.next;
}
str.append("}");
return str.toString();
}
}