-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomizedQueue.java
115 lines (92 loc) · 3.23 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* *****************************************************************************
* Name:
* Date:
* Description:
**************************************************************************** */
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import java.util.Iterator;
public class RandomizedQueue<Item> implements Iterable<Item> {
private Item[] a;
private int size;
public RandomizedQueue() {
a = (Item[]) new Object[2];
size = 0;
} // construct an empty randomized queue
private void resize(int capacity) {
Item[] temp = (Item[]) new Object[capacity];
for (int i = 0; i < size; i++) {
temp[i] = a[i];
}
a = temp;
}
public boolean isEmpty() {
return size == 0;
} // is the randomized queue empty?
public int size() {
return size;
} // return the number of items on the randomized queue
public void enqueue(Item item) {
if (item == null) throw
new java.lang.IllegalArgumentException();
if (size == a.length) resize(2 * a.length);
a[size++] = item;
} // add the item
public Item dequeue() {
if (isEmpty()) throw
new java.util.NoSuchElementException();
int randomIndex = StdRandom.uniform(size);
Item item = a[randomIndex];
a[randomIndex] = null;
size--;
if (size == 0) a = (Item[]) new Object[2];
else {
for (int i = randomIndex; i < size; i++) {
a[i] = a[i + 1];
}
a[size] = null;
}
return item;
} // remove and return a random item
public Item sample() {
if (isEmpty()) throw
new java.util.NoSuchElementException();
int randomIndex = StdRandom.uniform(size);
Item item = a[randomIndex];
return item;
} // return a random item (but do not remove it)
public Iterator<Item> iterator() {
return new RandomQueueIterator();
}
private class RandomQueueIterator<Item> implements Iterator<Item> {
private int i;
public RandomQueueIterator() {
i = 0;
}
@Override
public boolean hasNext() {
return i < size;
}
@Override
public Item next() {
if (!hasNext()) throw new java.util.NoSuchElementException();
return (Item) a[i++];
}
@Override
public void remove() {
throw
new java.lang.UnsupportedOperationException("remove() method is not supported");
}
} // return an independent iterator over items in random order
public static void main(String[] args) {
RandomizedQueue<String> stack = new RandomizedQueue<>();
for (int i = 0; i < args.length; i++) {
String item = args[i];
if (!item.equals("-"))
stack.enqueue(item);
else if (!stack.isEmpty())
StdOut.print(stack.dequeue() + " ");
}
StdOut.println("(" + stack.size() + " left on stack)");
} // unit testing (optional)
}