-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.pde
63 lines (55 loc) · 1.37 KB
/
Queue.pde
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
import java.util.LinkedList;
class Queue<T> {
private LinkedList<T> Queue;
/* creates a new empty Queue.
*/
public Queue() {
Queue = new LinkedList<T>();
}
/* returns true if and only if this Queue is empty.
*/
public boolean isEmpty() {
return (Queue.size() == 0);
}
/* adds the provided element to the back of the Queue.
*/
public void add(T e) {
Queue.add(e);
}
/* returns the front element of the Queue, or null if the Queue is empty.
*/
public T peek() {
return Queue.peek();
}
/* returns the front element of the Queue and removes that element from the Queue.
* Returns null if the Queue is empty.
*/
public T poll() {
return Queue.poll();
}
}
class QueueTester {
public void test() {
int metabolism = 3;
int vision = 2;
int initialSugar = 4;
Agent a1 = new Agent(metabolism, vision, initialSugar, new PollutionMovementRule());
Agent a2 = new Agent(metabolism, vision, initialSugar, new PollutionMovementRule());
Queue<Agent> q = new Queue<Agent>();
q.add(a1);
q.add(a1);
q.add(a2);
Agent a = q.peek();
assert(a.equals(a1));
a = q.poll();
assert(a.equals(a1));
a = q.poll();
assert(a.equals(a1));
assert(!q.isEmpty());
a = q.poll();
assert(a.equals(a2));
assert(q.isEmpty());
a = q.poll();
assert(a == null);
}
}