-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
54 lines (45 loc) · 1.28 KB
/
Queue.java
File metadata and controls
54 lines (45 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
/**
* A linked implementation of the Queue data structure.
* @author Samuel Heath 21725083, Bryan Trac 21704976
*/
public class Queue {
private Link first;
private Link last;
public Queue() {
first = null;
last = new Link(null,first);
}
public Object dequeue() throws Exception {
if (!isEmpty()) {
Object o = first.item;
first = first.successor;
if (isEmpty()) last = null;
return o;
} else throw new Exception("There are no elements in the Queue");
}
public Object examine() throws Exception {
if (!isEmpty()) {
return first.item;
} else throw new Exception("There are no elements in the Queue");
}
public void enqueue(Object o) {
if (isEmpty()) {
first = new Link(o,null);
last = first;
} else {
last.successor = new Link(o,null);
last = last.successor;
}
}
public boolean isEmpty() {
return first == null;
}
private class Link {
private Object item;
private Link successor;
public Link(Object item, Link successor) {
this.item = item;
this.successor = successor;
}
}
}