-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizedQueue.java
More file actions
137 lines (109 loc) · 2.56 KB
/
RandomizedQueue.java
File metadata and controls
137 lines (109 loc) · 2.56 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
import java.util.Iterator;
import edu.princeton.cs.algs4.StdRandom;
public class RandomizedQueue<Item> implements Iterable<Item> {
private Item [] s;
private int count;
private int capacity;
private static int InitialCapacity = 16;
private class ArrayIterator implements Iterator<Item> {
private int length;
private Item [] array;
ArrayIterator() {
array = (Item[]) new Object[count];
for (int i=0; i<count; i++) {
array[i] = s[i];
}
length = count;
}
@Override
public boolean hasNext() {
return length > 0;
}
@Override
public Item next() {
if (!hasNext()) {
throw new java.util.NoSuchElementException();
}
int index = StdRandom.uniform(0, length);
Item ret = array[index];
array[index] = array[length-1];
array[length-1] = null;
length--;
return ret;
}
}
// construct an empty randomized queue
public RandomizedQueue() {
capacity = InitialCapacity;
count = 0;
s = (Item [])new Object[capacity];
}
// is the randomized queue empty?
public boolean isEmpty() {
return count == 0;
}
// return the number of items on the randomized queue
public int size() {
return count;
}
// add the item
public void enqueue(Item item) {
if (item == null) {
throw new java.lang.IllegalArgumentException();
}
s[count] = item;
count++;
if (count >= capacity) {
capacity = capacity * 2;
resize(capacity);
}
}
// remove and return a random item
public Item dequeue() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
int index = StdRandom.uniform(0, count);
Item ret = s[index];
s[index] = s[count-1];
s[count-1] = null;
count--;
if (capacity > InitialCapacity && count <= capacity / 4) {
capacity = capacity / 2;
resize(capacity);
}
return ret;
}
// return a random item (but do not remove it)
public Item sample() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
int index = StdRandom.uniform(0, count);
return s[index];
}
// return an independent iterator over items in random order
public Iterator<Item> iterator() {
return new ArrayIterator();
}
private void resize(int size) {
Item copy[] = (Item []) new Object [size];
for (int i=0; i<count; i++) {
copy[i] = s[i];
}
s = copy;
}
// unit testing (optional)
public static void main(String[] args) {
RandomizedQueue<Integer> rq = new RandomizedQueue<>();
rq.enqueue(1);
rq.enqueue(2);
rq.enqueue(3);
rq.enqueue(4);
rq.enqueue(5);
Iterator<Integer> a = rq.iterator();
while (a.hasNext()) {
System.out.println(a.next());
}
}
}