-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_48_CircularQueue.java
103 lines (93 loc) · 2.68 KB
/
Day_48_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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.TrX;
public class Day_48_CircularQueue {
int [] arr;
int Front;
int Rear;
int size;
public Day_48_CircularQueue(int size){
this.arr = new int[size];
this.size=size;
this.Front = -1;
this.Rear = -1;
System.out.println("Circular Queue is Created with size "+size);
}
//isEmpty method to check weather the Queue is empty
public boolean isEmpty(){
if(Rear == -1){
return true;
}
return false;
}
//isFull method to check weather the Queue is full
public boolean isFull(){
if(Rear+1 == Front){
return true;
} else if (Front == 0 && Rear+1 == size) {
return true;
}
return false;
}
//enQueue method to insert element to the Queue
public void enQueue(int value){
if(isFull()){
System.out.println("The Queue is full!");
} else if (isEmpty()){
Front = 0;
Rear++;
arr[Rear] = value;
System.out.println("Successfully inserted "+value+" int the Queue");
}
else{
if(Rear == size){
Rear = 0;
} else{
Rear++;
}
arr[Rear] = value;
System.out.println("Successfully inserted "+value+" int the Queue");
}
}
//deQueue method to delete an element from Queue
public int deQueue(){
if(isEmpty()){
System.out.println("The Queue is empty!");
return -1;
}
else {
int element = arr[Front];
arr[Front] = Integer.MIN_VALUE;
if(Front == Rear){
Front = -1;
Rear = -1;
} else if (Front+1 == size){
Front=0;
}else{
Front++;
}
return element;
}
}
//peek method to see the 1st element in the Queue
public int peek(){
if(isEmpty()){
System.out.println("The Queue is Empty");
return -1;
}
return arr[Front];
}
//delete method
public void Delete(){
arr = null;
System.out.println("The Queue is successfully deleted!");
}
public static class CircularQueueTest {
public static void main(String[] args) {
Day_48_CircularQueue cQueue1 = new Day_48_CircularQueue(5);
cQueue1.enQueue(10);
cQueue1.enQueue(20);
cQueue1.enQueue(30);
cQueue1.enQueue(40);
cQueue1.enQueue(50);
}
}
}