-
Notifications
You must be signed in to change notification settings - Fork 0
/
RinqQueue.java
64 lines (55 loc) · 1.5 KB
/
RinqQueue.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
package com.company2;
import java.util.*;
/**
* Created by Benjamin R. on 31-07-2016.
*/
public class RinqQueue implements Queue {
static public final int DEFAULT_CAPACITY= 8;
private Object[] queue;
private int front;
private int rear;
private int capacity;
private int size = 0;
public RinqQueue(int cap) {
capacity = cap;
front = 0;
rear = capacity - 1;
queue= new Object[capacity];
}
public RinqQueue() {
this( DEFAULT_CAPACITY );
}
public void add(Object o) {
queue[front] = o;
}
public Object remove() throws NoSuchElementException {
if ( isEmpty() )
throw new NoSuchElementException();
else {
return front = 0;
}
}
public boolean isEmpty() {
return queue.length == 0;
}
public void clear() {
queue = new Object[0];
}
private void grow() {
Object[] old= queue;
int oldCapacity= capacity;
capacity *= 2;
queue= new Object[capacity];
if ( size == 0 )
return;
if ( front <= rear ) {
System.arraycopy(old, front, queue, 0, size );
} else if ( rear < front ) {
int nFront= oldCapacity-front;
System.arraycopy(old, front, queue, 0, nFront);
System.arraycopy(old, 0, queue, nFront, size-nFront );
}
front= 0;
rear= size-1;
}
}