forked from destiny1020/algorithm_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedQueue.java
More file actions
61 lines (52 loc) · 775 Bytes
/
LinkedQueue.java
File metadata and controls
61 lines (52 loc) · 775 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package chap1;
import java.util.Iterator;
public class LinkedQueue<Item> implements Iterable<Item>
{
private Node<Item> tail;
private Node<Item> head;
private int N;
public boolean isEmpty()
{
return (0 == N);
}
public int size()
{
return N;
}
public void enqueue(Item item)
{
Node<Item> oldLast = tail;
tail = new Node<Item>();
tail.item = item;
tail.next = null;
if(0 == N)
{
head = tail;
}
else
{
oldLast.next = tail;
}
N++;
}
public Item dequeue()
{
if(this.isEmpty())
{
return null;
}
Item item = head.item;
head = head.next;
if(this.isEmpty())
{
tail = null;
}
N--;
return item;
}
@Override
public Iterator<Item> iterator()
{
return new LinkedListIterator<Item>(head);
}
}