generated from Suvechchhaa/HacktoberFest-DSA2022
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCircularQueue.java
51 lines (43 loc) · 874 Bytes
/
CircularQueue.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
public class CircularQueue{
public static void main(String[] args) {
CQueue nums = new CQueue();
nums.add(10);
nums.printQueue();
}
}
class CQueue{
protected int[] arr;
protected int size = 0;
protected int start = 0;
protected int end = 0;
public CQueue(){
this.arr = new int[10];
}
public CQueue(int n){
this.arr = new int[size];
}
public int status(){
if(size == arr.length)return 1;
if(size == 0)return 0;
return -1;
}
public boolean add(int n){
if(status() == 1) return false;
size++;
arr[end++] = n;
end = end%arr.length;
return true;
}
public int remove(){
if(status() == 0) return -1;
start++;
size--;
start=start%arr.length;
return 1;
}
public void printQueue(){
for (int i=start; i <= end ; i++ ) {
System.out.print(arr[i] + " ");
}
}
}