Skip to content

Commit e6143a8

Browse files
Merge pull request #1 from pavanvattikala/Create-0622-design-circular-queue.java
Create 0622-design-circular-queue.java
2 parents 46af331 + 946f851 commit e6143a8

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
class MyCircularQueue {
2+
Node head=null;
3+
Node tail=null;
4+
5+
int currentLength=0;
6+
int totalLength=0;
7+
public MyCircularQueue(int k) {
8+
head=new Node(-1);
9+
tail=new Node(-1);
10+
11+
head.next=tail;
12+
head.prev=tail;
13+
14+
tail.next=head;
15+
tail.prev =head;
16+
17+
totalLength=k;
18+
}
19+
20+
public boolean enQueue(int value) {
21+
if(isFull()){
22+
return false;
23+
}
24+
25+
Node left = head.prev;
26+
Node right= head;
27+
28+
Node temp = new Node(value);
29+
30+
temp.next= right;
31+
32+
right.prev = temp;
33+
34+
left.next=temp;
35+
36+
temp.prev=left;
37+
38+
currentLength++;
39+
40+
41+
42+
return true;
43+
}
44+
45+
public boolean deQueue() {
46+
if(isEmpty()){
47+
return false;
48+
}
49+
50+
Node left = tail;
51+
Node right= tail.next.next;
52+
53+
left.next = right;
54+
55+
right.prev = left;
56+
57+
58+
currentLength--;
59+
60+
return true;
61+
}
62+
63+
public int Front() {
64+
if(currentLength<=0){
65+
return -1;
66+
}
67+
68+
return tail.next.val;
69+
}
70+
71+
public int Rear() {
72+
if(currentLength<=0){
73+
return -1;
74+
}
75+
76+
return head.prev.val;
77+
}
78+
79+
public boolean isEmpty() {
80+
return currentLength==0 ? true : false;
81+
}
82+
83+
public boolean isFull() {
84+
return currentLength==totalLength ? true : false;
85+
}
86+
87+
}
88+
89+
class Node{
90+
int val;
91+
Node next=null;
92+
Node prev=null;
93+
94+
Node(int val){
95+
this.val=val;
96+
}
97+
}

0 commit comments

Comments
 (0)