-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeqQueue.java
60 lines (51 loc) · 1.19 KB
/
SeqQueue.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
package sjjg;
public final class SeqQueue<T> implements Queue<T> {
private Object[] element;
private int front,rear;
public SeqQueue(int length) {
if(length<64)
length = 64;
this.element = new Object[length];
this.front = this.rear =0;
}
public SeqQueue(){
this(64);
}
@Override
public boolean isEmpty() {
// TODO 自动生成的方法存根
return this.front == this.rear;
}
@Override
public boolean add(T x) {
// TODO 自动生成的方法存根
if(x==null)
return false;
if(this.front == (this.rear+1)%this.element.length){
Object[] temp = this.element;
this.element = new Object[temp.length*2];
int j=0;
for(int i =this.front;i!=this.rear;i=(i+1)%temp.length)
this.element[j++] = temp[i];
this.front = 0;
this.rear = j;
}
this.element[this.rear] = x;
this.rear = (this.rear+1)%this.element.length;
return true;
}
@Override
public T peek() {
// TODO 自动生成的方法存根
return this.isEmpty()?null:(T)this.element[this.front];
}
@Override
public T poll() {
// TODO 自动生成的方法存根
if(this.isEmpty())
return null;
T temp = (T)this.element[this.front];
this.front = (this.front+1)%this.element.length;
return temp;
}
}