-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueuelab.java
More file actions
61 lines (52 loc) · 1.28 KB
/
Queuelab.java
File metadata and controls
61 lines (52 loc) · 1.28 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
/**
Author: Rajin Santos Gajadhar
Student ID: 239479650
Lab 8
Any and all work in this file is my own.
*/
public class Queuelab {
private SingleLinkedList list;
public Queuelab() {
this.list = new SingleLinkedList();
}
public boolean isEmpty() {
return list.size() == 0;
}
public void enqueue(String item) {
list.addLast(item);
}
public String dequeue() {
if (isEmpty()) {
throw new QueueException("Queue is empty. Cannot dequeue.");
}
return list.removeFirst();
}
public void dequeueAll() {
while (!isEmpty()) {
dequeue();
}
}
public String peek() {
if (isEmpty()) {
throw new QueueException("Queue is empty. Cannot peek.");
}
return list.getFirst();
}
public int size() {
return list.size();
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
try {
sb.append(list.get(i));
} catch (ListException e) {
e.printStackTrace();
}
if (i < list.size() - 1) {
sb.append(", ");
}
}
return sb.toString();
}
}