Add Circular Queue (#2528)

This commit is contained in:
Paridhi Jain 2021-10-12 12:03:10 +05:30 committed by GitHub
parent 0145352120
commit ad81d0ef45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,115 @@
//This program implements the concept of CircularQueue in Java
//Link to the concept: (https://en.wikipedia.org/wiki/Circular_buffer)
public class CircularQueue {
public static void main(String[] args) {
circularQueue cq= new circularQueue(5);
System.out.println(cq.isEmpty());
System.out.println(cq.isFull());
cq.enQueue(1);
cq.enQueue(2);
cq.enQueue(3);
cq.enQueue(4);
cq.enQueue(5);
System.out.println(cq.deQueue());
System.out.println(cq.deQueue());
System.out.println(cq.deQueue());
System.out.println(cq.deQueue());
System.out.println(cq.deQueue());
System.out.println(cq.isFull());
System.out.println(cq.isEmpty());
cq.enQueue(6);
cq.enQueue(7);
cq.enQueue(8);
System.out.println(cq.peek());
System.out.println(cq.peek());
cq.deleteQueue();
}
}
class circularQueue{
int[] arr;
int topOfQueue;
int beginningOfQueue;
int size;
public circularQueue(int size){
arr=new int[size];
topOfQueue=-1;
beginningOfQueue=-1;
this.size=size;
}
public boolean isEmpty(){
if(beginningOfQueue==-1){
return true;
}else{
return false;
}
}
public boolean isFull(){
if(topOfQueue+1==beginningOfQueue){
return true;
}else if(topOfQueue==size-1 && beginningOfQueue==0){
return true;
}else{
return false;
}
}
public void enQueue(int value){
if(isFull()){
System.out.println("The Queue is full!");
}
else if(isEmpty()) {
beginningOfQueue=0;
topOfQueue++;
arr[topOfQueue]=value;
System.out.println(value+" has been successfully inserted!");
}else{
if(topOfQueue+1==size){
topOfQueue=0;
}else{
topOfQueue++;
}
arr[topOfQueue]=value;
System.out.println(value+" has been successfully inserted!");
}
}
public int deQueue(){
if(isEmpty()){
System.out.println("The Queue is Empty!");
return -1;
}else{
int res= arr[beginningOfQueue];
arr[beginningOfQueue]=Integer.MIN_VALUE;
if(beginningOfQueue==topOfQueue){
beginningOfQueue=topOfQueue=-1;
}else if(beginningOfQueue+1==size){
beginningOfQueue=0;
}else{
beginningOfQueue++;
}
return res;
}
}
public int peek(){
if(isEmpty()){
System.out.println("The Queue is Empty!");
return -1;
}else{
return arr[beginningOfQueue];
}
}
public void deleteQueue(){
arr=null;
System.out.println("The Queue is deleted!");
}
}