Repository navigation
Expand file tree
/
Copy pathRandomizedQueue.java
More file actions
142 lines (119 loc) · 4.45 KB
/
Copy pathRandomizedQueue.java
File metadata and controls
142 lines (119 loc) · 4.45 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package queues;
import java.util.Iterator;
import java.util.NoSuchElementException;
import edu.princeton.cs.algs4.StdRandom;
public class RandomizedQueue<Item> implements Iterable<Item> {
private Item[] rq = null;
private int N = 0;
// construct an empty randomized queue
public RandomizedQueue() {
rq = (Item[]) new Object[1];
}
// is the queue empty?
public boolean isEmpty() {
return N == 0;
}
private void resizing(int capacity) {
Item[] copy = (Item[]) new Object[capacity];
for (int i = 0; i < N; i++) {
copy[i] = rq[i];
}
rq = copy;
}
// return the number of items on the queue
public int size() {
return N;
}
// add the item
public void enqueue(Item item) {
if (null == item) {
throw new NullPointerException();
}
if (N == rq.length) {
resizing(2 * rq.length);
}
rq[N] = item;
N++;
}
// remove and return a random item
public Item dequeue() {
if (N == 0) {
throw new NoSuchElementException();
}
if (N > 0 && N == (rq.length / 4)) {
resizing(rq.length / 2);
}
int randomNum = StdRandom.uniform(0, N);
Item retItem = rq[randomNum];
copyAfter(randomNum);
N--;
return retItem;
}
private void copyAfter(int index) {
for (int i = index; i < N - 1; i++) {
rq[i] = rq[i + 1];
}
}
// return (but do not remove) a random item
public Item sample() {
if (N == 0) {
throw new NoSuchElementException();
}
int randomNum = StdRandom.uniform(0, N);
Item retItem = rq[randomNum];
return retItem;
}
// return an independent iterator over items in random order
public Iterator<Item> iterator() {
return new RandomizedQueueIterator();
}
// unit testing
public static void main(String[] args) {
RandomizedQueue<Integer> integerRandom = new RandomizedQueue<>();
System.out.println(integerRandom.isEmpty());
integerRandom.enqueue(10);
integerRandom.enqueue(20);
integerRandom.enqueue(11);
integerRandom.enqueue(13);
System.out.println(integerRandom.isEmpty());
System.out.println(integerRandom.size());
Iterator<Integer> iterator = integerRandom.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
Integer dequeue = integerRandom.dequeue();
System.out.println("dequeue" + dequeue);
System.out.println(integerRandom.size());
for (Integer integer : integerRandom) {
System.out.println(integer);
}
}
private class RandomizedQueueIterator implements Iterator<Item> {
private int[] randomIndex = null;
private int index = 0;
RandomizedQueueIterator() {
randomIndex = new int[N];
for (int i = 0; i < N; i++) {
randomIndex[i] = i;
}
StdRandom.shuffle(randomIndex);
}
@Override
public boolean hasNext() {
return index != randomIndex.length;
}
@Override
public Item next() {
if (index == randomIndex.length) {
throw new NoSuchElementException();
}
Item item = rq[randomIndex[index]];
index++;
return item;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
}