forked from gaurav03kr/hello-hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular_queue.java
81 lines (79 loc) · 1.78 KB
/
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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com;
/*
*
* @author zenalarifin
*/
public class Program
{
public static void main(String[] args) {
CQueue c1 = new CQueue(4);
c1.add(1);
c1.add(2);
c1.add(3);
c1.add(4);
c1.add(5);
c1.display();
c1.delete();
c1.add(6);
c1.display();
}
}
class CQueue {
int capacity, front, end;
int arr[];
CQueue(int size ) {
front = -1;
end = -1;
capacity = size;
arr = new int[size];
}
void add(int item) {
if((front==capacity-1 && end==0) || end == front+1) {
System.out.println("Overflow");
}
else {
if(front== -1 && end == -1) {
front = 0;
end = 0;
}
else {
front++;
if(front == capacity) {
front = front - capacity;
}
}
arr[front] = item;
}
}
void delete() {
if(front==-1 && end==-1) {
System.out.println("underflow");
}
else {
if(front==end) {
front = -1;
end = -1;
}
else {
end++;
if(end==capacity) {
end=end-capacity;
}
}
}
}
void display() {
if(front==-1 && end==-1) {
System.out.println("Underflow");
}
else {
for(int i=front;i<=end;i++) {
System.out.println(arr[i]);
}
}
}
}