-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomizedQueue.java
95 lines (79 loc) · 2.44 KB
/
RandomizedQueue.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
import java.util.Iterator;
import java.util.NoSuchElementException;
public class RandomizedQueue<Item> implements Iterable<Item> {
private Item[] items;
private int initialCapacity = 10;
private int N;
// construct an empty randomized queue
public RandomizedQueue() {
items = (Item[]) new Object[initialCapacity];
}
// is the queue empty?
public boolean isEmpty() {
return N == 0;
}
// return the number of items on the queue
public int size() {
return N;
}
// add the item
public void enqueue(Item item) {
if (item == null) throw new NullPointerException();
if (N == items.length) resize(2 * items.length);
items[N++] = item;
}
// delete and return a random item
public Item dequeue() {
if (isEmpty()) throw new NoSuchElementException();
int i = StdRandom.uniform(N);
Item item = items[i];
exch(i, N - 1);
items[N - 1] = null;
N--;
if (N > 0 && N == items.length / 4) resize(items.length / 2);
return item;
}
// return (but do not delete) a random item
public Item sample() {
if (isEmpty()) throw new NoSuchElementException();
int i = StdRandom.uniform(N);
return items[i];
}
// return an independent iterator over items in random order
public Iterator<Item> iterator() {
final Item[] tmp = (Item[]) new Object[N];
System.arraycopy(items, 0, tmp, 0, N);
StdRandom.shuffle(tmp);
return new Iterator<Item>() {
Item[] iteratorItems = tmp;
int cur;
@Override
public boolean hasNext() {
return cur != iteratorItems.length;
}
@Override
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
return iteratorItems[cur++];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
private void resize(int capacity) {
assert capacity >= N;
Item[] temp = (Item[]) new Object[capacity];
System.arraycopy(items, 0, temp, 0, N);
items = temp;
}
private void exch(int i, int j) {
Item tmp = items[i];
items[i] = items[j];
items[j] = tmp;
}
// unit testing
public static void main(String[] args) {
}
}