-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizedQueue.java
More file actions
147 lines (126 loc) · 4.05 KB
/
RandomizedQueue.java
File metadata and controls
147 lines (126 loc) · 4.05 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
143
144
145
146
147
/* *****************************************************************************
* Name: Courtney Hoppus
* Date: 2021-01-15
* Description: Assignment 1
**************************************************************************** */
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class RandomizedQueue<Item> implements Iterable<Item> {
private static final int DEFAULT_CAPACITY = 1;
private int size;
private Item[] items;
// construct an empty randomized queue
public RandomizedQueue() {
this(DEFAULT_CAPACITY);
}
private RandomizedQueue(int capacity) {
size = 0;
items = (Item[]) new Object[capacity];
}
// is the randomized queue empty?
public boolean isEmpty() {
return size == 0;
}
// return the number of items on the randomized queue
public int size() {
return size;
}
// add the item
public void enqueue(Item item) {
if (item == null) {
throw new NullPointerException("item is null");
}
if (size == items.length) {
resize(2 * items.length);
}
items[size++] = item;
}
// remove and return a random item
public Item dequeue() {
if (isEmpty()) {
throw new NoSuchElementException();
}
int randomIdx = StdRandom.uniform(size);
Item item = items[randomIdx];
if (randomIdx != size - 1) {
items[randomIdx] = items[size - 1];
}
items[--size] = null;
if (size >= 1 && size == items.length / 4) {
resize(items.length / 2);
}
return item;
}
private void resize(int capacity) {
Item[] aux = (Item[]) new Object[capacity];
for (int idx = 0; idx < size; idx++) {
aux[idx] = items[idx];
}
items = aux;
}
// return a random item (but do not remove it)
public Item sample() {
if (isEmpty()) {
throw new NoSuchElementException();
}
int randomIdx = StdRandom.uniform(size);
return items[randomIdx];
}
// return an independent iterator over items in random order
public Iterator<Item> iterator() {
return new RandomQueueIterator();
}
private class RandomQueueIterator implements Iterator<Item> {
private final Item[] aux;
private int auxSize = size;
public RandomQueueIterator() {
aux = (Item[]) new Object[auxSize];
for (int idx = 0; idx < auxSize; idx++) {
aux[idx] = items[idx];
}
}
public boolean hasNext() {
return auxSize > 0;
}
public Item next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
int randomIdx = StdRandom.uniform(auxSize);
Item item = aux[randomIdx];
if (randomIdx != auxSize - 1) {
aux[randomIdx] = aux[auxSize - 1];
}
aux[--auxSize] = null;
if (item == null) {
return next();
}
return item;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
// unit testing (required)
public static void main(String[] args) {
RandomizedQueue<String> random = new RandomizedQueue<>();
random.enqueue("Betty Wetter");
random.enqueue("Cookie Couture");
random.enqueue("Beau Degas");
random.enqueue("Angel Baby");
random.enqueue("Arson Nicki");
random.enqueue("Miss Texas");
StdOut.println(random.size());
for (String next : random) {
StdOut.println(next);
}
StdOut.println(random.size());
StdOut.println("Is it empty = " + random.isEmpty());
StdOut.println(random.sample());
StdOut.println(random.dequeue());
StdOut.println(random.dequeue());
StdOut.println(random.size());
}
}