Skip to content

Commit 6cb7f79

Browse files
Merge pull request #76 from saideepthipadala/work
622. Design Circular Queue
2 parents 74ac67f + 99688bc commit 6cb7f79

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

MyCircularQueue.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//https://leetcode.com/problems/design-circular-queue/
2+
3+
4+
class MyCircularQueue {
5+
6+
public int [] arr;
7+
int front = 0,rear=-1;
8+
int num_of_elem =0;
9+
public MyCircularQueue(int k) {
10+
arr = new int[k];
11+
num_of_elem=k;
12+
}
13+
14+
public boolean enQueue(int value) {
15+
16+
if(!isFull()){
17+
rear = (rear+1)%num_of_elem;
18+
arr[rear]=value;
19+
return true;
20+
}
21+
else return false;
22+
23+
}
24+
25+
public boolean deQueue() {
26+
if(!isEmpty()){
27+
if(front == rear) {
28+
rear =-1;front = 0;
29+
}
30+
else {
31+
front =(front+1) %num_of_elem;
32+
}
33+
return true;
34+
}
35+
else return false;
36+
}
37+
38+
public int Front() {
39+
if(!isEmpty()) return arr[front];
40+
else return -1;
41+
42+
43+
}
44+
45+
public int Rear() {
46+
if(!isEmpty()) return arr[rear];
47+
else return -1;
48+
49+
}
50+
51+
public boolean isEmpty() {
52+
if(rear == -1) return true;
53+
else return false;
54+
55+
}
56+
57+
public boolean isFull() {
58+
if(front == (rear+1)%num_of_elem && !isEmpty()) return true;
59+
else return false;
60+
61+
}
62+
}

0 commit comments

Comments
 (0)