-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircleQueue.java
More file actions
89 lines (74 loc) · 1.89 KB
/
Copy pathCircleQueue.java
File metadata and controls
89 lines (74 loc) · 1.89 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
package Queue;
import org.omg.CORBA.Object;
public class CircleQueue<T> implements _Queue<T> {
public T[] arr;
public int size;
public int tail;
public int head;
public static final int DEFAULT = 10;
public CircleQueue() {
this.arr = (T[]) new Object[DEFAULT];
head = tail = 0;
}
public CircleQueue(int capacity) {
this.arr = (T[]) new Object[capacity];
head = tail = 0;
}
/*
环状会有一个unit space 不会被存储内容
cause: 差1 tail == head(队列空)
环状满时,(tail+1)% size = head
*/
public void getLargeQueue(int capacity) {
if (capacity < size)
return;
T[] old = this.arr;
this.arr = (T[]) new Object[capacity];
int j = 0;
//copy the element
for (int i = this.head;i != this.tail; i =(i+1) % old.length) {
arr[j++] = old[i];
}
this.head = 0;
this.tail = j;
}
@Override
public boolean isEmpty() {
return head == tail;
}
@Override
public int size() {
return size;
}
@Override
public boolean add(T data) {
//满环队列。
if (this.head == (this.tail + 1) % this.arr.length ) {
getLargeQueue(arr.length*2);
}
//添加data
arr[this.tail] = data;
this.tail = (this.tail+1) % arr.length;
size++;
return true;
}
@Override
public T peek() {
return arr[head];
}
@Override
public T poll() {
T temp = this.arr[this.head];
this.head = (this.head + 1) % this.arr.length;
size--;
return temp;
}
@Override
public void clear() {
for (int i = this.head ; i != this.tail; i=(i+1)%arr.length) {
arr[i] = null;
}
this.head = this.tail = 0;
size = 0;
}
}