Simple Queue implementation in java

    Queue is a collection of elements stored in sequential manner. It has a principle called FIFO (First In First Out). It means the element added in the queue is removed first.The implementation of queue is done easily using java.

The built-in packages in java for implementing queue is given below.

java.util.LinkedList

java.util.Queue

The implementation of simple queue is as follows.

It has 5 basic operations.

Enqueue : It adds the element to the queue.

Dequeue: It removes the element from the queue.

Size : It finds the size of the queue.

isEmpty: It checks the queue has elements or not.

peek() : It finds the front elements.

Steps to implement the program:

First, include the built-in packages.

Create the class  QueueSimple with main () function.

An object is created from builtin class “Queue” as integer.

Add the elements to the queue by using add() function.

Print the elements in the queue.

Using the peek () function to print the front element.

Pool() function is used to delete the element from the queue. This operation is called “Dequeue”.

To check the queue is empty or not, isEmpty() function is used.

Size() function is used to check the size of the queue.

Java code:

import java.util.LinkedList;

import java.util.Queue;

public class QueueSimple {

    public static void main(String[] args) {

        // let us create a queue

        Queue<Integer> queue1 = new LinkedList<>();

        // Insert elements to the queue(Enqueue)

        queue1.add(34);

        queue1.add(45);

        queue1.add(56);

        queue1.add(75);

        queue1.add(89);

        // Print the elements in the queue

        System.out.println("The elements in the Queue are: " + queue1);

        //Get the front element(Peek) and print it

        int front = queue1.peek();

        System.out.println("Front element of the queue is: " + front);

        // Remove elements(Dequeue) from the queue

        int del = queue1.poll();

        System.out.println("Deleted element: " + del);

        // print the queue elements after deleting (dequeuing) an element

        System.out.println("Queue after deletion: " + queue1);

        // Check whether the queue has no elements

        boolean isEmpty = queue1.isEmpty();

        if(isEmpty)

          System.out.println("The queue is empty");

        else

          System.out.println("The queue is not empty");

        // Find the size of the queue

        int size = queue1.size();

        System.out.println("Size of the queue is: " + size);

    }

}

Output:

C:\raji\blog>javac QueueSimple.java

C:\raji\blog>java QueueSimple

The elements in the Queue are: [34, 45, 56, 75, 89]

Front element of the queue is: 34

Deleted element: 34

Queue after deletion: [45, 56, 75, 89]

The queue is not empty

Size of the queue is: 4

This is the way of implementing  simple queue in java. If you have any queries, just drop a comment below.

Keep coding!!!

No comments:

Post a Comment