-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathEx_1_3_36.java
More file actions
114 lines (94 loc) · 2.17 KB
/
Ex_1_3_36.java
File metadata and controls
114 lines (94 loc) · 2.17 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package Ch_1_3;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import java.util.Iterator;
/**
* Created by HuGuodong on 2019/7/27.
*/
public class Ex_1_3_36 {
public static class _RandomQueue<Item> implements Iterable<Item> {
int N;
Item[] a;
public _RandomQueue() {
a = (Item[]) new Object[1];
}
private void resize(int cap) {
Item[] temp = (Item[]) new Object[cap];
for (int i = 0; i < N; i++) {
temp[i] = a[i];
}
a = temp;
}
public void enqueue(Item item) {
if (N == a.length) {
resize(2 * a.length);
}
a[N++] = item;
}
public Item dequeue() {
if (isEmpty()) {
return null;
}
if (N > 0 && N == a.length / 4) {
resize(a.length / 2);
}
int r = StdRandom.uniform(N);
Item item = a[r];
// delete
a[r] = a[N - 1];
a[N - 1] = null;
N--;
return item;
}
public Item sample() {
return a[StdRandom.uniform(N)];
}
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
public class RandomIterator implements Iterator<Item> {
Item[] copy;
int num = N;
int idx;
public RandomIterator() {
copy = (Item[]) new Object[num];
for (int i = 0; i < num; i++) {
copy[i] = a[i];
}
for (int i = 0; i < num; i++) {
int r = i + StdRandom.uniform(num - i); // between i~n-1
Item temp = copy[i];
copy[i] = copy[r];
copy[r] = temp;
}
}
@Override
public boolean hasNext() {
return idx < num;
}
@Override
public Item next() {
return copy[idx++];
}
}
@Override
public Iterator<Item> iterator() {
return new RandomIterator();
}
}
public static void main(String[] args) {
_RandomQueue<Integer> q = new _RandomQueue<>();
int N = 20;
for (int i = 0; i < N; i++) {
q.enqueue(i);
}
for (Integer i : q) {
StdOut.printf("%d ", i);
}
StdOut.println();
// 7 10 0 5 12 4 15 6 18 3 19 17 11 14 16 1 13 2 9 8
}
}