-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizedQueue.java
More file actions
135 lines (113 loc) · 3.44 KB
/
RandomizedQueue.java
File metadata and controls
135 lines (113 loc) · 3.44 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
package A02RandomizedQueuesAndDeques;
/**
* Created by Adam Gardner and Cesar Gonzalez on 10/8/16.
*/
import java.util.Iterator;
import java.util.NoSuchElementException;
import edu.princeton.cs.introcs.StdOut;
import edu.princeton.cs.introcs.StdRandom;
public class RandomizedQueue<Item> implements Iterable<Item> {
private int size = 0;
private Item[] array;
private int head;
private int tail = 0;
public RandomizedQueue(){
// construct an empty randomized queue
}
// is the queue empty?
public boolean isEmpty(){
return size == 0;
}
// return the number of items on the queue
public int size(){
return size;
}
// add the item
public void enqueue(Item item){
if(item == null){ throw new NullPointerException(); }
//init empty array
if(isEmpty()){
array = (Item[])(new Object[10]);
}
//Double array when full
if(size == array.length){
Item [] auxArray = (Item[])(new Object[size * 2]);
for(int i = 0; i < size; i++){
auxArray[i] = array[i];
}
array = auxArray;
}
size++;
array[tail] = item;
tail++;
}
// delete and return a random item
public Item dequeue(){
if(isEmpty()){ throw new NoSuchElementException(); }
//half array when only 1/4 full
if(size == array.length/4 ){
Item [] auxArray = (Item[])(new Object[array.length /2]);
for(int i = 0; i < size; i++){
auxArray[i] = array[i];
}
array = auxArray;
}
Item second = sample();
int location = tail - 1;
array[head] = array[location];
array[location] = null;
--tail;
--size;
return second;
}
// return (but do not delete) a random item
public Item sample(){
if(isEmpty()){ throw new NoSuchElementException(); }
head = StdRandom.uniform(tail);
return array[head];
}
// return an independent iterator over items in random order
public Iterator<Item> iterator(){
return new RandomizedQueueIterator<Item>(array, tail);
}
public class RandomizedQueueIterator<Item> implements Iterator<Item>{
Item[] current;
int head = -1;
int tail;
public RandomizedQueueIterator(Item[] array,int end){
current = array;
tail = end;
}
public boolean hasNext(){
return !(head == tail-1) ;
}
public Item next(){
if(!hasNext()) { throw new NoSuchElementException(); }
head++;
return current[head];
}
public void remove(){
throw new UnsupportedOperationException();
}
}
public static void main(String[] args){
// unit testing
RandomizedQueue<String> queue = new RandomizedQueue<>();
queue.enqueue("one");
queue.enqueue("two");
queue.enqueue("three");
queue.enqueue("four");
queue.enqueue("five");
//queue.dequeue();
//queue.dequeue();
StdOut.print("Dequeued: ");
StdOut.print(queue.dequeue() + ", ");
StdOut.print( queue.dequeue() + ", ");
StdOut.print( queue.dequeue() + " \n");
StdOut.println();
StdOut.print("Queue: ");
for(String el: queue){
StdOut.printf(el + ", ");
}
}
}