-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0622-design-circular-queue.java
97 lines (66 loc) · 1.5 KB
/
0622-design-circular-queue.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
86
87
88
89
90
91
92
93
94
95
96
97
class MyCircularQueue {
Node head=null;
Node tail=null;
int currentLength=0;
int totalLength=0;
public MyCircularQueue(int k) {
head=new Node(-1);
tail=new Node(-1);
head.next=tail;
head.prev=tail;
tail.next=head;
tail.prev =head;
totalLength=k;
}
public boolean enQueue(int value) {
if(isFull()){
return false;
}
Node left = head.prev;
Node right= head;
Node temp = new Node(value);
temp.next= right;
right.prev = temp;
left.next=temp;
temp.prev=left;
currentLength++;
return true;
}
public boolean deQueue() {
if(isEmpty()){
return false;
}
Node left = tail;
Node right= tail.next.next;
left.next = right;
right.prev = left;
currentLength--;
return true;
}
public int Front() {
if(currentLength<=0){
return -1;
}
return tail.next.val;
}
public int Rear() {
if(currentLength<=0){
return -1;
}
return head.prev.val;
}
public boolean isEmpty() {
return currentLength==0 ? true : false;
}
public boolean isFull() {
return currentLength==totalLength ? true : false;
}
}
class Node{
int val;
Node next=null;
Node prev=null;
Node(int val){
this.val=val;
}
}