-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArrayQueue.java
More file actions
64 lines (55 loc) · 1.99 KB
/
ArrayQueue.java
File metadata and controls
64 lines (55 loc) · 1.99 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
package org.usfirst.FTC5866.library;
/**
* Created by Olavi Kamppari on 10/7/2015.
*
* Added to Github on 11/16/2015 (https://github.com/OliviliK/FTC_Library/edit/master/ArrayQueue.java)
*/
public class ArrayQueue<AnyType> {
private AnyType[] queue;
private int queueSize;
private int head;
private int tail;
private static final int DEFAULT_SIZE = 4;
@SuppressWarnings("unchecked") // The array casting is OK
private AnyType[] newQueue(int size) {
return (AnyType[]) new Object[size];
}
public ArrayQueue() {
queue = newQueue(DEFAULT_SIZE);
queueSize = queue.length;
head = 0;
tail = 0;
}
public void close(){
while (!isEmpty()) remove();// Discard all elements
}
public boolean isEmpty() {return head == tail;}
public int length() {return (tail + queueSize - head) % queueSize;}
public void add(AnyType element) {
int nextTail = (tail +1) % queueSize;
if (nextTail == head) { // The queue is full
int i; // Double the queue size
AnyType[] nextQueue = newQueue(2 * queueSize);
// Copy the elements from the original queue
for (i = 0; head != tail; i++, head = (head + 1) % queueSize) {
nextQueue[i] = queue[head];
queue[head] = null; // Support garbage collection
}
queue = nextQueue;
queueSize = queue.length;
head = 0;
tail = i;
nextTail = (tail +1) % queueSize;
}
queue[tail] = element;
tail = nextTail;
}
public AnyType remove() {
AnyType element;
if (isEmpty()) return null;
element = queue[head];
queue[head] = null; // Enable garbage collection
head = (head + 1) % queueSize;
return element;
}
}