Circular Queue in java using array - Algomentor

Circular Queue in java using array



Hi, in this section we will learn about the circular queue in java using an array we learn why the circular queue is preferred above the queue because in the queue there is a memory wastage but in case of the circular queue we can reuse the waste memory, again and again, it is like merry go round we start with one place a move in the circular motion. After completely reading this article you have a deep inside of circular array you will be able to solve the problem of the circular queue. and able to implement a program of the circular queue.

circular queue in java using array

What are requisite for learning circular queue in java using array?

circular queue in java also knows as a circular buffer. concept of a circular queue is the same as queue first in first out (FIFO) concept. or you can say (LILO) last in last out this also contains two-pointer front and back as a queue has.

Here is the pictorial representation of the circular queue

circular queue in java using array example

Name the different types of queues?
  • simple queue
  • circular queue
  • priority queue
  • doubly ended queue

What is the main use of circular queue/buffer in java?

A circular queue is very much useful in the CPU scheduling algorithm like a round-robin algorithm in this algorithm a process is processed by CPU then if the process is not completed in the given time then the process joins the circular queue again.

circular queue helps us to manage the waste memory in the simple queue is a front pointer is mover forward all the backward spaces are free to use but we can't access them in a simple queue but circular queue can handle this and reuse the occupied space. In simple words, a circular queue helps you to reuse the waste location of an array.


Now we will start our coding section in this section we discuss code and how this coding is done for function one by one.

How to create a circular queue in java using array?

public class circularQueue
{
static int front=-1,rear=-1;
static int size=4;
}

we initialize two-variable front, rear with -1. and size variable with 4. I give size hear 4 but you can give any size according to your need so this is how we create a CircularQueue class in java.

Write the code of the push method of the circular queue in java?

public static void push(int[] queue,int data)
{
if((front == 0 && rear == size-1) || (rear == (front-1)%(size-1)))
{
System.out.println("Queue is full can't insert");
}
else if(front==-1)
{
front=rear=0;
queue[rear]=data;
}
else if(rear == (size-1) && front != 0)
{
rear=0;
queue[rear]=data;
}
else
{
rear++;
queue[rear]=data;
}
}

In this push method, we have four conditions in the first condition we check if the queue is full or not if first condition is true then we just print the message that "Queue is full can't insert" in the second case we check if front=-1 that means the queue is empty then we make front and rear equal to 0. and insert the element at 0 indexes of the queue array. in the third case, we check if the difference between front and rear is -1. and the front is not equal to 0 then we can insert an element at 0 indexes and hence we save the save memory by using previous memory. and the last case we will normally insert value in the queue.

Write the code of the pop method of the circular queue in java?

public static void pop(int[] queue)
{
if(front==-1)
{
System.out.println("queue is empty");
}
else if(front==rear)
{
front= -1;
rear= -1;
}
else if(front==size-1)
{
front=0;
}
else
{
front++;
}

}

Same as push method pop method of the circular queue is also have 4 different conditions in the first condition we check if the queue is empty or not if condition became true then we will print "Queue is empty" in the second condition if the front is equal to rear means the queue has only one element so we set front and rear to -1. in the third condition we check if the front is at last of the queue array then we set front to 0. and in the last condition we increase the front by 1.

Write the code of the peek method of the circular queue in java?

public static void peek(int[] queue)
{
System.out.println(queue[front]);
}

In this peek method we only need to print the element at the front index in the queue array.

Write the code of the print method of print the circular queue in java?

public static void print(int[] queue)
{
if(front==-1)
{
System.out.println("the queue is empty");
}
if (rear >= front) 
    for (int i = front; i <= rear; i++) 
    {
    System.out.print(queue[i]+" ");
    }   
else
     for (int i = front; i < size; i++)
    {
    System.out.print(queue[i]+" ");
    }  
    for (int i = 0; i <= rear; i++) 
    {
    System.out.print(queue[i]+" ");
    }
}
}

In this print method, we will print each element present in the circular queue so for that we need to go through each element one by one and print the element in circular we do this take by using three condition statements. in the first condition, we check if the queue is empty if the condition is true we simply print the message that queue is empty in the second condition we check if the rear is greater then the front if the condition is true then we need to run for loop to print each from front to rear.

And the third and last condition is very important if the front is greater then rear in that case you need run two for loop one from front to end of the array and another from 0 to rear index of the array and hence you can print the elements of the array in the circular queue in java.

Write the code of the search method of the circular queue in java to search an element?

public static void search(int[] queue,int data)
{
boolean flag = false;
if(front==-1)
{
System.out.println("the queue is empty");
}
if (rear >= front) 
    for (int i = front; i <= rear; i++) 
    {
    if(queue[i]==data)
    {
    flag = true;
    }
    }
        
else
        for (int i = front; i < size; i++)
    {
    if(queue[i]==data)
    {
    flag = true;
    }
    }  
    for (int i = 0; i <= rear; i++) 
    {
    if(queue[i]==data)
    {
    flag = true;
    }
    }
if(flag==true)
{
System.out.println("we found it...");
}
else
{
System.out.println("not found it...");
}
}

the search function is as same as the print function you only need to create a flag and initialize with false and after traverse from each element if you find your targeting element then make flag equal to true and at the last if your flag is the true print message that "Element find in the queue" otherwise print "Can't find element".

If you find difficult to understand some part of code then you should go through these articles.

however, these articles are written in c++ but the concept is the same and the structure of java and c++ is somewhat the same.

you can get the complete code with all 5 functions we discuss above in My Github you can visit there and download the complete code of queue in java implementations.

I hope you like our article on the queue and its implementations in java language. and if you like our article then share with your coder friends as well as.

Thank you so much for reading this article and if you have any suggestions or compliments for us you can contact us using the contact form showing on the right side of the page.

if you want to write a post on our website then please check this article on How to do a guest post.

Post a Comment

0 Comments