-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathQueue.java
100 lines (86 loc) · 2.04 KB
/
Queue.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.Iterator;
import java.util.Scanner;
public class Queue<Item> implements Iterable<Item>
{
private Node first; // 指向最早添加的结点的链接
private Node last; // 指向最近添加的结点的链接
private int N; // 队列中元素的数量
private class Node
{
Item item;
Node next;
}
public boolean isEmpty()
{
return first == null; // Or: N == 0.
}
public int size()
{
return N;
}
public void enqueue(Item item)
{ //向表尾添加元素
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty())
{
first = last;
}
else
{
oldlast.next = last;
}
N++;
}
public Item dequeue()
{ // 从表头删除元素
Item item = first.item;
first = first.next;
if (isEmpty())
{
last = null;
}
N--;
return item;
}
public Iterator<Item> iterator()
{
return new ListIterator();
}
private class ListIterator implements Iterator<Item> //实现迭代器
{
private Node current = first;
public boolean hasNext()
{
return current != null;
}
public void remove()
{
//null
}
public Item next()
{
Item item = current.item;
current = current.next;
return item;
}
}
public static void main(String[] args)
{ // Create a queue and enqueue/dequeue Double.
Queue<Double> q = new Queue<Double>();
String data = "10.0 20.0 30.0 40.0";
Scanner sc = new Scanner(data);
while (sc.hasNext())
{
q.enqueue(sc.nextDouble());
}
sc.close();
int N = q.size();
for (int i = 0; i < N; i++)
{
System.out.println(q.dequeue());
}
}
}