-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomizedQueue.java
82 lines (71 loc) · 2.35 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
import java.util.Iterator;
import edu.princeton.cs.algs4.StdRandom;
public class RandomizedQueue<Item> implements Iterable<Item>{
private Item[] s;
private int N = 0;
private int sz;
public RandomizedQueue(){
s =(Item[]) new Object[1]; //the Ugly Cast;
}
public void enqueue(Item item){
if(item == null)
throw new IllegalArgumentException("No argument");
if(N == s.length) resize(2 * s.length);
s[N++] = item;
sz++;
}
private void resize(int len){
Item[] copy =(Item[]) new Object[len];
if(len>s.length){len = s.length;}
for(int i=0;i<len;i++){copy[i] = s[i];}
s = copy;
}
public boolean isEmpty(){
return N == 0;
}
public Item dequeue(){
if(isEmpty())
throw new java.util.NoSuchElementException("empty queue");
int pop = StdRandom.uniform(N);
Item item = s[pop];
s[pop] = s[--N];
s[N] = null;
sz--;
if(N>0 && N==s.length/4){ resize(s.length/2);}
return item;
}
public int size(){ return sz; }
public Item sample(){
if(isEmpty())
throw new java.util.NoSuchElementException("empty queue");
return s[StdRandom.uniform(N)];
}
public Iterator<Item> iterator(){ return new ReverseArrayIterator(); }
private class ReverseArrayIterator implements Iterator<Item>{ //unclear about what implement does!! type-cast??
private int i = N;
public boolean hasNext(){ return i > 0;}
public void remove() {
throw new UnsupportedOperationException("Not Supported");
/* not supported */
}
public Item next() {
if(!hasNext())
throw new java.util.NoSuchElementException("End of queue");
int pop = StdRandom.uniform(i);
Item item = s[pop];
s[pop] = s[--i];
return item;
}
}
public static void main(String[] args){
//unit testing
RandomizedQueue<String> D = new RandomizedQueue<>();
for(String x: args){
D.enqueue(x);
}
Iterator<String> iterator = D.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}