-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyCircularQueue.java
85 lines (76 loc) · 1.93 KB
/
MyCircularQueue.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
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
package org.sean.queue;
/***
* 622. Design Circular Queue
*
* <p>
* Implementation of the MyCircularQueue class:
* </p>
*
* <img src="../../../../../../images/buffer.png">
*/
public class MyCircularQueue {
private final int size;
private final int[] queue;
private int tear = 0;
private int front = 0;
public MyCircularQueue(int k) {
if (k <= 0)
throw new IllegalArgumentException("The queue size should be larger than 0");
size = k + 1;
queue = new int[size];
}
/***
* Inserts an element into the circular queue. Return true if the operation is successful.
* @param value
* @return
*/
public boolean enQueue(int value) {
boolean notFull = !isFull();
if (notFull) {
queue[tear] = value;
tear = (tear + 1) % size;
}
return notFull;
}
/***
* Deletes an element from the circular queue. Return true if the operation is successful.
* @return
*/
public boolean deQueue() {
boolean notEmpty = !isEmpty();
if (notEmpty) {
front = (front + 1) % size;
}
return notEmpty;
}
/***
* Gets the front item from the queue. If the queue is empty, return -1.
* @return
*/
public int Front() {
if (isEmpty()) return -1;
return queue[front % size];
}
/***
* Gets the last item from the queue. If the queue is empty, return -1.
* @return
*/
public int Rear() {
if (isEmpty()) return -1;
return queue[(tear + size - 1) % size];
}
/***
* Checks whether the circular queue is empty or not.
* @return
*/
public boolean isEmpty() {
return front % size == tear % size;
}
/***
* Checks whether the circular queue is full or not.
* @return
*/
public boolean isFull() {
return (tear + 1) % size == front % size;
}
}