-
Notifications
You must be signed in to change notification settings - Fork 3
/
Circular_Queue.java
74 lines (68 loc) · 1.7 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
package DATA_STRUCTURE;
public class Circular_Queue {
static final int max = 5;
int front = -1;int rear = -1;
int []queue = new int[max];
// boolean isFull(){
// if((rear+1)%max == front){
// System.out.println("Overflow!");
// return true;
// }
// return false;
// }
void insert(int d){
if((rear+1)%max == front){
System.out.println("Queue is full");
}
else if(front == -1 && rear == -1){
front = rear = 0;
}
else if ((rear == max-1) && (front!=0)){
rear = 0;
}
else{
rear = (rear+1)%max;
}
queue[rear] = d;
}
void display(){
if(front == -1){
System.out.println("Underflow");
}
else{
System.out.println("Element present ");
for (int i = front; i <=rear ; i++) {
{
System.out.println(queue[i]);
}
}
}
}
void delete(){
if(front == -1){
System.out.println("Queue is empty..!");
}
else if(front == rear){
front = rear =-1;
}
else{
int n = queue[front];
front = (front+1)%max;
System.out.println(n+" Deleted! form queue");
}
}
public static void main(String[] args) {
Circular_Queue cq = new Circular_Queue();
cq.insert(14);
cq.insert(15);
cq.insert(16);
cq.insert(17);
cq.insert(18);
// cq.delete();
cq.delete();
// cq.delete();
cq.delete();
cq.display();
System.out.println("\n\n");
}
}