-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
46 lines (40 loc) · 722 Bytes
/
Queue.java
File metadata and controls
46 lines (40 loc) · 722 Bytes
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
public class Queue {
private class Node {
private Object value;
private Node next;
}
private Node first, last;
public void add(Object v) {
Node n = new Node();
n.value = v;
if( first == null )
first = n;
if( last == null )
last = n;
else {
last.next = n;
last = n;
}
}
public Object remove() {
if( first == null )
return null;
Object v = first.value;
first = first.next;
if( first == null )
last = null;
return v;
}
public boolean isEmpty() {
return first == null;
}
public static void main(String[] args) {
Queue s = new Queue();
for(int i=0; i<10; i++) {
s.add(i);
}
while( !s.isEmpty() ) {
System.out.println("" + s.remove() + " ");
}
}
}