Queue in java using array - Algomentor

Queue in java using an array with solved example

Hi, friends in this article we will learn about the queue in java with a solved example we will learn about how the queue works what are the use cases of the queue in coding as well as some real-life application of the queue we will learn queue in java so you should have a basic knowledge of java like what is class, what is a method, how to create them,  and how to call them, etc.

queue in java with example

So in this article, we will create a queue in java using an array but we can also create a queue with the help of a singly linked list, We will discuss linked queue in some another article we not only learn theory but we will learn how to implement a queue in java language also we will create push method, pop method, top method, or empty method in the queue we will discuss all these methods by creating a separate method for all in the single class named queue. I hope you are interested in the queue and we will read the whole article.

Some important topic you can learn before learning queue
All these articles link above if you read all of them we surely help you get a good helping hand to learn queue as we implement a queue in java using array so you also have good inside or array you can read the article by clicking on above link.

What is a queue in java?

The queue is a linear data structure where the element is inserted from last and coming out from the front end we can say queue is a (FIFO) first in first out and (LILO) last in last out. you can imagine as queue as a line of people standing in front of a temple where each person go to last in the line and move forward till all the person ahead left the queue.

Here is the pictorial presentation of the queue.

queue in java using array explained

What are the different types of the queue?
  • Simple queue
  • Circular queue
  • Priority queue
  • Doubly ended queue
What are the ways to implement the queue?
What is the main use of the queue?

The queue is very much useful in CPU scheduling, Disk scheduling you are doing multitasking in you PC and simultaneously open much software like browser, calculator, photos, and editing software and your CPU in giving time to them one by one and when CPU gives process them software once it will put software at the end of queue and process other software and this process is doing again and again till the user finish the software and where software or process is killed by the user then CPU put out from the queue and this is how CPU work two you can imagine how CPU work without a queue.

Why do we need a queue?

There is much application of queue in a nonlinear data structure such as graphs without queue you can not implement graph algorithms like Breadth-first search which is very common algorithms in graphs and very useful in many aspects of graphs theory.

Which is a better option among stack or the queue?

See both stack and queue are better to depend on what you are doing if you inverse a string stack is a better option or if you process a string queue is a better option so it is totally your choice because both have their own application.

Now we will start our coding section we will learn about core queue function using java language.

How to create a queue in java?

public class queue
{
static int front=-1,rear=-1;
}

you just need to create a class with any name in my case I give the name of this class to queue. and create two static variables named front and rear and initialize with -1. and create an array in the main function and your basic structure of queue is ready now we will do the operation in a queue.

What is a push method in the queue? 

public static void push(int[] queue,int data)
{
if(front==rear && front==-1)
{
front=0;
rear = 0;
queue[rear]=data;
}
else
{
rear++;
queue[rear]=data;
}
}

In this push method, we pass two arguments one is an array with a named queue. another is data we need to insert in the queue. first if conditional it is checking if the queue is empty or not if the queue is empty then make front and rear is equal to 0 and put data at the rear end. in the second condition, if the queue is not empty means we increase the rear pointer and then insert the value at the rear end of the queue.


What is the pop method in the queue?

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

In the pop method, we have three conditions first is if the queue is empty then we just print the message "underflow" in the second case if the front is equal to the rear mean there is only one element in the queue and we need to pop it out so we make front and rear both equal to -1 (means making queue empty) and in the third condition if there are more then one element in the array we just need to increase the front pointer.

What is the peek method in the queue?

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

In the peek method we just print the element in the front position without deleting any of the elements.

How to print the queue in java?

public static void print(int[] queue)
{
if(front == rear && front==-1)
{
System.out.println("There is no element in the queue");
}
else
{
for(int i= front;i<=rear;i++)
{
System.out.print(queue[i]+" ");
}
}
}

For the printing element of the queue, we first need to check if the queue is empty or not if it is empty then we need to print the message. that "There is no element in the queue" in the second case if the queue is not empty then we need to run a loop from front to rear and print the element in the queue.

How we can search for an element in the queue?

public static void search(int[] queue,int data)
{
boolean flag = false;
if(front == rear && front==-1)
{
System.out.println("queue not exist");
}
for(int i=front;i<=rear;i++)
{
if(queue[i]==data)
{
flag = true;
}
}
if(flag == true)
{
System.out.println("element in the queue...");
}
else
{
System.out.println("element not in the queue...");
}
}

For searching in the queue in java we first need to create a boolean flag and initialize with false we will move from front to rear and check if element exists we make flag to true. and after the loop ends we check if the flag is true means element exists and if the flag is false means element not exist. 

you can get the complete code with all 5 functions we discuss above in My Github you can visit their 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