-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCircularQueue.java
More file actions
86 lines (62 loc) · 1.78 KB
/
Copy pathCircularQueue.java
File metadata and controls
86 lines (62 loc) · 1.78 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
class MyCircularQueue {
private int[] queue;
private int head, tail;
public MyCircularQueue(int k) {
this.queue = new int[k];
this.head = -1;
this.tail = -1;
}
public boolean enQueue(int value) {
if (isFull()){
return false;
}
else if (isEmpty()){
this.head++;
this.queue[++this.tail] = value;
}
else {
this.tail = (this.tail + 1) % queue.length;
queue[this.tail] = value;
}
return true;
}
public boolean deQueue() {
if (isEmpty()) {
return false;
}
else if (this.head == this.tail) {
this.head = -1;
this.tail = -1;
}
else {
this.head = (this.head + 1) % this.queue.length;
}
return true;
}
public int Front() {
return isEmpty() ? -1 : this.queue[this.head];
}
public int Rear() {
return isEmpty() ? -1 : this.queue[this.tail];
}
public boolean isEmpty() {
if (this.head == -1 && this.tail == -1){
return true;
} else {
return false;
}
}
public boolean isFull() {
return ((this.tail + 1) % this.queue.length) == this.head;
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* 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();
*/