-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueArray.java
More file actions
79 lines (62 loc) · 1.54 KB
/
Copy pathQueueArray.java
File metadata and controls
79 lines (62 loc) · 1.54 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
package Queue;
public class QueueArray implements Queue{
private static final int CAP=7;//队列默认大小
private Object[] elements;
private int capacity;
private int front;
private int rear;
public QueueArray (int cap) {
this.capacity=cap+1;
this.elements=new Object[this.capacity];
this.front=0;
this.rear=0;
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return (rear-front+capacity)%capacity;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return front==rear;
}
@Override
public void enqueue(Object e) {
// TODO Auto-generated method stub
if(getSize()==capacity-1) expandSpace();
elements[rear]=e;
rear=(rear+1)%capacity;
}
private void expandSpace() {
// TODO Auto-generated method stub
Object[] a=new Object[elements.length*2];
int i=front;
int j=0;
while(i!=rear) {
a[j++]=elements[i];
i=(i+1)%capacity;
}
elements=a;
capacity=elements.length;
front=0;
rear=j;
}
@Override
public Object dequeue() throws QueueEmptyException {
// TODO Auto-generated method stub
if(isEmpty())
throw new QueueEmptyException("错误,空队列");
Object obj=elements[front];
elements[front]=null;
front=(front+1)%capacity;
return obj;
}
@Override
public Object peek() throws QueueEmptyException {
// TODO Auto-generated method stub
if(isEmpty())
throw new QueueEmptyException("错误,空队列");
return elements[front];
}
}