-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResizingArrayQueue.java
107 lines (91 loc) · 3.33 KB
/
ResizingArrayQueue.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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Fundamentals;
/**
*
* @author stefan0
*/
/*************************************************************************
* Compilation: javac ResizingArrayQueue.java
* Execution: java ResizingArrayQueue < input.txt
* Data files: http://algs4.cs.princeton.edu/13stacks/tobe.txt
*
* Queue implementation with a resizing array.
*
* % java ResizingArrayQueue < tobe.txt
* to be or not to be (2 left on queue)
*
*************************************************************************/
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayQueue<Item> implements Iterable<Item> {
private Item[] q; // queue elements
private int N = 0; // number of elements on queue
private int first = 0; // index of first element of queue
private int last = 0; // index of next available slot
// cast needed since no generic array creation in Java
public ResizingArrayQueue() {
q = (Item[]) new Object[2];
}
public boolean isEmpty() { return N == 0; }
public int size() { return N; }
// resize the underlying array
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;
}
public void enqueue(Item item) {
// double size of array if necessary and recopy to front of array
if (N == q.length) resize(2*q.length); // double size of array if necessary
q[last++] = item; // add item
if (last == q.length) last = 0; // wrap-around
N++;
}
// remove the least recently added item
public Item dequeue() {
if (isEmpty()) throw new RuntimeException("Queue underflow");
Item item = q[first];
q[first] = null; // to avoid loitering
N--;
first++;
if (first == q.length) first = 0; // wrap-around
// shrink size of array if necessary
if (N > 0 && N == q.length/4) resize(q.length/2);
return item;
}
public Iterator<Item> iterator() { return new ArrayIterator(); }
// an iterator, doesn't implement remove() since it's optional
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;
}
}
/**
* A test client.
*/
public static void main(String[] args) {
ResizingArrayQueue<String> q = new ResizingArrayQueue<String>();
/*
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) q.enqueue(item);
else if (!q.isEmpty()) StdOut.print(q.dequeue() + " ");
}
StdOut.println("(" + q.size() + " left on queue)");
*/
}
}