-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.java
294 lines (230 loc) · 7.15 KB
/
Queue.java
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
292
293
294
//---------------------------------------------
// NAME: Oghenetega Okene
// OBJECTIVE: Implement a Queue ADT
//---------------------------------------------
/*
* This Queue ADT stores
* a String along with a Queue of ints
* in each element of the Queue.
* This Queue class contains a Node class that
* stores the Queue of ints, and the
* Queue of ints class uses another
* Node class that stores it's int values
*/
public class Queue {
// Instance variables
private Node front; // top of queue
private Node end; // end of queue
private int distinctWords; // number of distint words in our Queue
// Constructor
public Queue() {
front = end = null;
}
// Instance methods
/*
* Add a numnber to the end of the queue
*/
public void enqueue(String word, int lineNum) {
if (front == null) { // if the queue is empty
front = end = new Node(word);
(front.queue).enqueue(lineNum); // add to the int queue
distinctWords++; // the word is not in the Queue
} else if (front.next == null) { // only 1 item in the list
Node curr = search(word);
if (curr == null) { // the word is not on the queue -> make a newNode
Node newNode = new Node(word);
end = newNode;
front.next = end;
(end.queue).enqueue(lineNum);
distinctWords++; // the word is not in the Queue
} else { // add to the list of integer queues in the Node
(curr.queue).enqueue(lineNum); // add to the int queue
}
} else { // more than one item in the list
Node curr = search(word);
if (curr == null) { // the word is not on the queue
Node newNode = new Node(word);
end.next = newNode;
end = newNode;
(end.queue).enqueue(lineNum); // add to the int queue
distinctWords++; // the word is not in the Queue
} else { // the word is in the queue
(curr.queue).enqueue(lineNum); // add to the int queue
}
}
}
/*
* Delete and return the Node
* at the front of the queue
*/
public Node dequeue() {
Node curr = front;
if (curr != null) { // at least 1 item in the list
if (curr.next == null) { // only one item left
emptyQueue();
} else {
front = front.next;
}
}
return curr;
}
/*
* Search the Queue for a String value (word)
* returning the Node where "word" is found
* and null otherwise
*/
public Node search(String word) {
Node curr = front;
while (curr != null && !(curr.word).equals(word)) {
curr = curr.next;
}
return curr;
}
/*
* Helper toString method
* to see program behaviour
*/
public String toString() {
String output = "<";
Node curr = front;
while (curr != null) {
output += curr.word + ",";
curr = curr.next;
}
if (output.length() > 2) {
output = output.substring(0, output.length() - 1); // remove extra comma
}
output += "<";
return output;
}
/*
* Prints out the contents of the main Queue
* and the Queue containing ints that
* accompanies the main Queue
*/
private void printResults() {
Node curr = front;
System.out.println("\nThere are a total of " + distinctWords + " invalid words:\n");
while (curr != null) {
System.out.print("Invalid word " + "\"" + (dequeue().word) + "\"");
System.out.print(" found on lines ");
IntQueue currQueue = curr.queue;
IntNode currNode = currQueue.front;
while (currNode != null) {
System.out.print((currQueue.deqeue()).number + " ");
currNode = currNode.next;
}
curr = curr.next;
System.out.println();
}
System.out.println();
}
/*
* empty the contents of the queue
*/
public void emptyQueue() {
front = end = null;
}
/*
* private Node class
* used by the main Queue
* class to store a word and
* an Queue of ints
*/
private class Node {
// Instance Variables
private String word;
private Node next;
private IntQueue queue;
// Constructor
public Node(String word) {
this.word = word;
this.queue = new IntQueue();
}
}
/*
* This class is used to store the
* queue of ints in our Queue class
*/
private class IntQueue {
// Instance variables
private IntNode front; // top of queue
private IntNode end; // end of queue
// Constructor
public IntQueue() {
front = end = null;
}
// Instance methods
/*
* Add a numnber to the end of the queue
*/
public void enqueue(int number) {
if (front == null && end == null) { // empty list
front = end = new IntNode(number, front);
} else {
IntNode newNode = new IntNode(number, null);
end.next = newNode;
end = newNode;
if (front.next == null) {// only one item in the list
front.next = end;
}
}
}
/*
* Delete and return the int
* at the front of the queue
*/
public IntNode deqeue() {
IntNode deletednode = null;
if (!isEmpty()) { // queue is not empty
deletednode = front; // we will always delete from the front
if (front.next == null) { // only one item left
emptyQueue();
} else {
front = front.next;
}
}
return deletednode;
}
// self explanatory
public boolean isEmpty() {
return front == null;
}
/*
* empty the contents of the queue
*/
public void emptyQueue() {
front = end = null;
}
/*
* Helper toString method
* to see program behaviour
*/
public String toString() {
String output = "<";
IntNode curr = front;
while (curr != null) {
output += curr.number + ",";
curr = curr.next;
}
output = output.substring(0, output.length() - 1); // remove extra comma
output += "<";
return output;
}
}
/*
* Node containing an int value
* and next IntNode. This class is
* used by IntQueue
*/
private class IntNode {
// Instance Variables
private int number;
private IntNode next;
// Constructor
public IntNode(int number, IntNode next) {
this.number = number;
this.next = next;
}
}
}