-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizedQueue.java
More file actions
123 lines (107 loc) · 2.55 KB
/
Copy pathRandomizedQueue.java
File metadata and controls
123 lines (107 loc) · 2.55 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
package coursera;
import java.util.Iterator;
public class RandomizedQueue<Item> implements Iterable<Item> {
private int size;
private Item[] arr;
public RandomizedQueue() { // construct an empty randomized queue
this.arr = (Item[]) new Object[1];
this.size = 0;
}
public boolean isEmpty() { // is the randomized queue empty?
return size == 0;
}
public int size() { // return the number of items on the randomized queue
return size;
}
public void enqueue(Item item) { // add the item
if (item == null) {
throw new java.lang.IllegalArgumentException();
}
if (size == arr.length) {
this.resize(2 * size);
}
arr[size++] = item;
}
public Item dequeue() { // remove and return a random item
if (size == 0) {
throw new java.util.NoSuchElementException();
}
int i = (int) (Math.random() * size);
if (size == arr.length / 4) {
resize(arr.length / 2);
}
size--;
Item it = arr[i];
arr[i] = arr[size];
arr[size] = null;
return it;
}
public Item sample() { // return a random item (but do not remove it)
if (size == 0) {
throw new java.util.NoSuchElementException();
}
int i = (int) (Math.random() * size);
Item it = arr[i];
return it;
}
private void shuffle() {
if (size > 1) {
for (int i = 0; i < size; i++) {
int temp = (int) (Math.random() * (i + 1));
Item it1 = arr[i];
arr[i] = arr[temp];
arr[temp] = it1;
}
}
}
public Iterator<Item> iterator() { // return an independent iterator over items in random order
this.shuffle();
return new Iterator<Item>() {
int index = 0;
@Override
public boolean hasNext() {
if (index >= size)
return false;
else
return true;
}
@Override
public Item next() {
if (index >= size) {
throw new java.util.NoSuchElementException();
}
Item it = arr[index];
index++;
return it;
}
public void remove() {
throw new java.lang.UnsupportedOperationException();
}
};
}
private void resize(int n) {
Item[] newArr = (Item[]) new Object[n];
for (int i = 0; i < size; i++) {
newArr[i] = this.arr[i];
}
this.arr = newArr;
}
public static void main(String[] args) { // unit testing (optional)
RandomizedQueue<String> rq = new RandomizedQueue<>();
rq.enqueue("sss");
rq.enqueue("aaa");
rq.enqueue("bbb");
rq.enqueue("ccc");
rq.enqueue("ddd");
rq.enqueue("eee");
Iterator it = rq.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println();
Iterator it2 = rq.iterator();
while (it2.hasNext()) {
System.out.println(it2.next());
}
}
}