-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMyCycleQueue.java
More file actions
78 lines (71 loc) · 1.11 KB
/
MyCycleQueue.java
File metadata and controls
78 lines (71 loc) · 1.11 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
package ch03;
/*
* 列队类
*/
public class MyCycleQueue {
//底层使用数组
private long[] arr;
//有效数据的大小
private int elements;
//队头
private int front;
//队尾
private int end;
/**
* 默认构造方法
*/
public MyCycleQueue() {
arr = new long[10];
elements = 0;
front = 0;
end = -1;
}
/**
* 带参数的构造方法,参数为数组的大小
*/
public MyCycleQueue(int maxsize) {
arr = new long[maxsize];
elements = 0;
front = 0;
end = -1;
}
/**
* 添加数据,从队尾插入
*/
public void insert(long value) {
if(end == arr.length - 1) {
end = -1;
}
arr[++end] = value;
elements++;
}
/**
* 删除数据,从队头删除
*/
public long remove() {
long value = arr[front++];
if(front == arr.length) {
front = 0;
}
elements--;
return value;
}
/**
* 查看数据,从队头查看
*/
public long peek() {
return arr[front];
}
/**
* 判断是否为空
*/
public boolean isEmpty() {
return elements == 0;
}
/**
* 判断是否满了
*/
public boolean isFull() {
return elements == arr.length;
}
}