-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinearFrequencyTable.java
More file actions
212 lines (170 loc) · 5.3 KB
/
LinearFrequencyTable.java
File metadata and controls
212 lines (170 loc) · 5.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
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;
//my own variables
//private int counterThing;// not sure hwta i want to do with it
//private LinkedList<String> listyList;//my list
//listyList=new LinkedList<String>;
//private long[] countArray;//mt array
/** 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) {
if (key == null){
throw new IllegalArgumentException("key is null");
}
Elem p = head.next;
while(p.key!=null){
if(key.compareTo(p.key)==0){//if they are the same
return p.count;
}
p=p.next;
}
throw new NoSuchElementException("Key not Found");
//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) {
if (key == null){
throw new IllegalArgumentException("key is null");
}
Elem p = head.next;
//if(p.next==null){
while(p.key!=null){
int test = key.compareTo(p.key); //compare object with current node
if (test==0){//the same
throw new IllegalArgumentException("key already there");
}p=p.next;
}
if(head.next==head){
head.next= new Elem(key, head, head.next);
head.previous=head.next;
}else{
Elem init = head.next; //point to dummy element
while(init != head && init.key.compareTo(key) < 0){ //while value less then key
init=init.next;
}
Elem next = init.next; //what wouldve come after the init elem
init.next = new Elem(key, init, next);
next.previous = init.next;
}size++;
}
/** 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");
Elem update = head.next;
while(update != head && update.key.compareTo(key)!= 0){ //havent hit the dummy yet,
update=update.next; //running through this loop with my woes
}
if (update==head){
throw new NoSuchElementException("Key not Found");
}else{//else update count
update.count++;
}
}
/** 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() {
LinkedList<String> listyList;
listyList=new LinkedList<String>();//heres where i make a new list thingy, cause it makes sense to me
Elem runner=head.next;
while(runner.key!=null){
listyList.addLast(runner.key);
runner=runner.next;
}
//throw new UnsupportedOperationException("IMPLEMENT THIS METHOD");
return listyList;
}
/** 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() {
long[] countArray;
countArray=new long[size];//creates the array to hold it all
int counter=0;
Elem p= head.next;
while(p!=head){
countArray[counter]=p.count;
p=p.next;
counter++;
}
return countArray;
//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();
}
/*
private boolean compareTo(Node<E> p){
return this.key>p.key;
}
*/
}