Java Program to Check Whether a Queue Is Empty or Not

You are currently viewing Java Program to Check Whether a Queue Is Empty or Not

Java program to check whether a queue is empty or not

// Java program to check a queue is empty or not

import java.util.LinkedList;
import java.util.Queue;

public class Main {
  public static void main(String[] args) {
    Queue < Integer > queue = new LinkedList < > ();
    Queue < Integer > emQueue = new LinkedList < > ();

    queue.add(10);
    queue.add(20);
    queue.add(30);
    queue.add(40);
    queue.add(50);

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

    if (emQueue.isEmpty())
      System.out.println("The emQueue is empty.");
    else
      System.out.println("The emQueue is not empty.");
  }
}

Output

The queue is not empty.
The emQueue is empty.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply