-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizedQueue.java
More file actions
129 lines (122 loc) · 3.48 KB
/
RandomizedQueue.java
File metadata and controls
129 lines (122 loc) · 3.48 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
/*
* Randomized queue. A randomized queue is similar to a stack or queue,
* except that the item removed is chosen uniformly at random from items
* in the data structure.
* 28-08-2012.
*/
/*
* Failed on 156th operation: returned null on call to dequeue
* 1000 random calls (p1 = 0.9, p2 = 0.1)
Failed on 138th operation: returned null on call to dequeue
*/
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;
public class RandomizedQueue<Item> implements Iterable<Item> {
private Random rand = new Random();
private Item[] q;
private int N = 0;
private int first = 0;
private int last = 0;
// construct an empty randomized queue
public RandomizedQueue()
{
q = (Item[]) new Object[2];
}
// is the queue empty?
public boolean isEmpty()
{
return N == 0;
}
// return the number of items on the queue
public int size()
{
return N;
}
private void resize(int max)
{
assert max >= N;
Item[] temp = (Item[]) new Object[max];
for (int i = 0; i < N; i++)
temp[i] = q[(first + i) % q.length];
q = temp;
first = 0;
last = N;
}
// add the item
public void enqueue(Item item)
{
if (item == null)
throw new NullPointerException();
if (N == q.length)
resize(2*q.length);
q[last++] = item;
N++;
}
// delete and return a random item
public Item dequeue()
{
if (isEmpty())
throw new NoSuchElementException("Queue underflow");
Random randomGenerator = new Random();
int randomIndex = rand.nextInt(N);
Item item = q[randomIndex];
if (randomIndex != N-1)
{
q[randomIndex] = q[N-1];
q[N-1] = null;
}
else
q[randomIndex] = null;
N--;
last--;
if (N > 0 && N == q.length/4)
resize(q.length/2);
return item;
}
// return (but do not delete) a random item
public Item sample()
{
if (isEmpty())
throw new NoSuchElementException("Queue underflow");
rand = new Random();
int randomIndex = rand.nextInt(N);
Item item = q[randomIndex];
return item;
}
public Iterator<Item> iterator()
{
return new ArrayIterator();
}
private class ArrayIterator implements Iterator<Item> {
private int i = 0;
public boolean hasNext()
{
return i < N;
}
public void remove()
{
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext())
throw new NoSuchElementException();
Item item = q[(i + first) % q.length];
i++;
return item;
}
}
public static void main(String[] args) {
RandomizedQueue<String> q = new RandomizedQueue<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (item.equals("-"))
StdOut.println("item removed: " + q.dequeue());
else if (item.equals("s"))
StdOut.println("sample item: " + q.sample());
else
q.enqueue(item);
StdOut.println("(" + q.size() + " left on queue)");
}
}
}