-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQueueLL.java
More file actions
62 lines (52 loc) · 1.23 KB
/
Copy pathQueueLL.java
File metadata and controls
62 lines (52 loc) · 1.23 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
62
import java.util.Iterator;
import java.lang.Iterable;
public class QueueLL<Item> implements Iterable<Item>
{
public Iterator<Item> iterator() { return new QueueLLIterator(); }
private class QueueLLIterator implements Iterator<Item>
{
private Node current = first;
public boolean hasNext()
{
return current != null;
}
public Item next()
{
Item item = current.item;
current = current.next;
return item;
}
public void remove()
{
// not implemented
}
}
private class Node
{
Item item;
Node next;
public Node(Item item)
{
this.item = item;
}
}
private Node first, last;
public boolean isEmpty()
{
return first == null;
}
public void enqueue(Item item)
{
Node oldlast = last;
last = new Node(item);
if (isEmpty()) first = last;
else oldlast.next = last;
}
public Item dequeue()
{
Item item = first.item;
first = first.next;
if (isEmpty()) last = first;
return item;
}
}