-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignCircularQueue.java
More file actions
132 lines (112 loc) · 3.03 KB
/
DesignCircularQueue.java
File metadata and controls
132 lines (112 loc) · 3.03 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package leetcode;
/**
* DesignCircularQueue
* https://leetcode.cn/problems/design-circular-queue/
* 622. 设计循环队列
* https://leetcode.cn/problems/design-circular-queue/solution/mo-ni-by-oshdyr-1r3g/
*
* @author Tobin
* @since 2022-08-02
*/
public class DesignCircularQueue {
public static void main(String[] args) {
// MyCircularQueue obj = new MyCircularQueue(k);
// boolean param_1 = obj.enQueue(value);
// boolean param_2 = obj.deQueue();
// int param_3 = obj.Front();
// int param_4 = obj.Rear();
// boolean param_5 = obj.isEmpty();
// boolean param_6 = obj.isFull();
MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3
System.out.println(circularQueue.enQueue(1)); // 返回 true
System.out.println(circularQueue.enQueue(2)); // 返回 true
System.out.println(circularQueue.enQueue(3)); // 返回 true
System.out.println(circularQueue.enQueue(4)); // 返回 false,队列已满
System.out.println(circularQueue.Rear()); // 返回 3
System.out.println(circularQueue.isFull()); // 返回 true
System.out.println(circularQueue.deQueue()); // 返回 true
System.out.println(circularQueue.enQueue(4)); // 返回 true
System.out.println(circularQueue.Rear()); // 返回 4
}
}
class MyCircularQueue {
private int length;
private int currSize;
private int[] queue;
private int start; // 0
private int next; // 0
public MyCircularQueue(int k) {
length = k;
currSize = 0;
queue = new int[length];
start = 0;
next = 0;
}
/**
* 向循环队列插入一个元素。如果成功插入则返回真
*
* @param value
* @return
*/
public boolean enQueue(int value) {
if (isFull()) {
return false;
}
queue[next] = value;
next = (next + 1) % length;
currSize++;
return true;
}
/**
* 从循环队列中删除一个元素。如果成功删除则返回真
*
* @return
*/
public boolean deQueue() {
if (isEmpty()) {
return false;
}
queue[start] = -1;
start = (start + 1) % length;
currSize--;
return true;
}
/**
* 从队首获取元素。如果队列为空,返回 -1
*
* @return
*/
public int Front() {
if (isEmpty()) {
return -1;
}
return queue[start];
}
/**
* 获取队尾元素。如果队列为空,返回 -1
*
* @return
*/
public int Rear() {
if (isEmpty()) {
return -1;
}
return queue[(next - 1 + length) % length];
}
/**
* 检查循环队列是否为空
*
* @return
*/
public boolean isEmpty() {
return currSize < 1;
}
/**
* 检查循环队列是否已满
*
* @return
*/
public boolean isFull() {
return currSize >= length;
}
}