forked from Anuj-Kumar-Sharma/Java-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingCircularArray.java
More file actions
48 lines (40 loc) · 851 Bytes
/
Copy pathQueueUsingCircularArray.java
File metadata and controls
48 lines (40 loc) · 851 Bytes
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
package queueBasics;
public class QueueUsingCircularArray {
int front, rear;
int a[];
int n;
public QueueUsingCircularArray(int n) {
front = rear = -1;
a = new int[n];
this.n = n;
}
void enqueue(int data) throws Exception {
if(isFull()) throw new Exception("Queue Array is Full");
if(isEmpty()) {
front = 0;
}
rear = (rear+1)%n;
a[rear] = data;
}
int dequeue() throws Exception {
if(isEmpty()) throw new Exception("Queue Array is Empty");
if(front == rear) { // to check if one element only
int ans = a[front];
front = -1;
rear = -1;
return ans;
}
int ans = a[front];
front = (front+1) % n;
return ans;
}
int getSize() {
return isEmpty() ? 0 : (n-front+rear) % n + 1;
}
private boolean isFull() {
return (rear+1) % n == front;
}
boolean isEmpty() {
return front == -1;
}
}