-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeque.java
41 lines (35 loc) · 843 Bytes
/
Deque.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
public class Deque extends CircularQueue {
Deque() {
front = -1;
rear = -1;
}
protected void frontAdd(int val) {
if (isFull()) {
System.out.println("Deque is full");
return;
} else if (isEmpty()) {
front = 0;
rear = 0;
} else if (front == 0) {
front = len -1;
} else {
front -= 1;
}
queue[front] = val;
}
protected void frontRemove() {
dequeue();
}
protected void rearAdd(int val) {
enqueue(val);
}
protected void rearRemove() {
if (isEmpty()) {
System.out.println("Queue is empty");
} else if (rear == 0) {
rear = len - 1;
} else {
rear -= 1;
}
}
}