- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleQueue.java
More file actions
Latest commit
92 lines (79 loc) · 2.28 KB
/
Copy pathDoubleQueue.java
File metadata and controls
92 lines (79 loc) · 2.28 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
classMyCircularQueue {
privateint[] circleQueue; //queue
privateintfront; //front pos
privateintrear; //rear pos
privateintsize;
/** Initialize your data structure here. Set the size of the queue to be k. */
publicMyCircularQueue(intk) {
size = k;
circleQueue = newint[size];
front = -1;
rear = -1;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
publicbooleanenQueue(intvalue) {
if (isFull()) {
returnfalse;
}
if (isEmpty()) {
front = 0;
}
rear = (rear + 1) % size;
circleQueue[rear] = value;
returntrue;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
publicbooleandeQueue() {
if (isEmpty()) {
returnfalse;
}
if (front == rear) {
front = -1;
rear = -1;
returntrue;
}
front = (front + 1) % size;
returntrue;
}
/** Get the front item from the queue. */
publicintFront() {
if (isEmpty()) {
return -1;
}
returncircleQueue[front];
}
/** Get the last item from the queue. */
publicintRear() {
if (isEmpty()) {
return -1;
}
returncircleQueue[rear];
}
/** Checks whether the circular queue is empty or not. */
publicbooleanisEmpty() {
returnrear == -1;
}
/** Checks whether the circular queue is full or not. */
publicbooleanisFull() {
if (front == (rear + 1) % size) {
returntrue;
}
returnfalse;
}
publicstaticvoidmain(String[] args) {
intk = 6;
MyCircularQueueobj = newMyCircularQueue(k);
System.out.println(obj.enQueue(6));
System.out.println(obj.Rear());
System.out.println(obj.Rear());
obj.deQueue();
booleanparam_3 = obj.enQueue(5);
System.out.println(param_3);
System.out.println(obj.Rear());
obj.deQueue();
System.out.println(obj.Front());
obj.deQueue();
obj.deQueue();
obj.deQueue();
}
}