English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented Programming(I)

Java Object-Oriented Programming(II)

Java Object-Oriented Programming(III)

Java Exception Handling

Java List(List)

Java Queue(Queue)

Java Map Collection

Java Set Collection

Java Input Output (I/O)

Java Reader/Writer

Java Other Topics

Java Program to Implement Queue (Queue) Data Structure

    Java Examples Comprehensive

In this example, we will learn to implement a queue data structure using Java.

To understand this example, make sure you first visit the following tutorial,

Example1: A Java program to implement a stack

public class Queue {
  int SIZE = 5;
  int items[] = new int[SIZE];
  int front, rear;
  Queue() {
    front = -1;
    rear = -1;
  }
  //Check if the queue is full
  boolean isFull() {
    if (front == 0 && rear == SIZE - 1) {
      return true;
    }
    return false;
  }
  //Check if the queue is empty
  boolean isEmpty() {
    if (front == -1])
      return true;
    else
      return false;
  }
  //Insert elements into the queue
  void enQueue(int element) {
    //If the queue is full
    if (isFull()) {
      System.out.println("Queue is full");
    }
    else {}}
      if (front == -1) {
        //Mark front to indicate the first element of the queue
        front = 0;
      }
      rear++;
      //Insert element at the end
      items[rear] = element;
      System.out.println("Insert "); + element);
    }
  }
  //Delete element from the queue
  int deQueue() {
    int element;
    //If the queue is empty
    if (isEmpty()) {
      System.out.println("Queue is empty");
      return (-1);
    }
    else {}}
      //Delete element from the front of the queue
      element = items[front];
      //If there is only one element in the queue
      if (front >= rear) {
        front = -1;
        rear = -1;
      }
      else {}}
        //Mark the next element as front
        front++;
      }
      System.out.println(element + "Deleted");
      return (element);
    }
  }
  //Display queue elements
  void display() {
    int i;
    if (isEmpty()) {
      System.out.println("Empty Queue");
    }
    else {}}
      //Display the front of the queue
      System.out.println("\nFront index-> " + front);
      //Display the elements of the queue
      System.out.println("Items -> ");
      for (i = front; i <= rear; i++])
        System.out.print(items[i + "    ");
      // Display the rear of the queue
      System.out.println("\nRear index-> " + rear);
    }
  }
  public static void main(String[] args) {
    //Create an object of the Queue class
    Queue q = new Queue();
    //Attempt to delete an element from the queue
    // The current queue is empty
    // Therefore, it cannot be deleted
    q.deQueue();
    // Insert elements into the queue
    for(int i = 1; i < 6; i ++) {
      q.enQueue(i);
    }
    // Cannot insert the6elements cannot be added to the queue because it is full
    q.enQueue(6);
    q.display();
    // deQueue deletes the first input element, for example:1
    q.deQueue();
    //Now we only have4elements
    q.display();
  }
}

Output Result

Queue is empty
Insert 1
Insert 2
Insert 3
Insert 4
Insert 5
Queue is full
Front index-> 0
Items ->
1  2  3  4  5  
Rear index-> 4
1 Deleted
Front index-> 1
Items ->
2  3  4  5
Rear index-> 4

In the above example, we have implemented the queue data structure using Java.

Example2: Implementing a stack using the Queue interface

Java provides a built-in interface Queue that can be used to implement queues.

import java.util.Queue;
import java.util.LinkedList;
class Main {
  public static void main(String[] args) {
    //Create a queue using the LinkedList class
    Queue<Integer> numbers = new LinkedList<>();
    // enqueue
    //Insert element at the end of the queue
    numbers.offer(1);
    numbers.offer(2);
    numbers.offer(3);
    System.out.println("Queue: ", + numbers);
    // dequeue
    //Delete element from the front of the queue
    int removedNumber = numbers.poll();
    System.out.println("Deleted element: ", + removedNumber);
    System.out.println("Remove and queue after: ") + numbers);
    }
}

Output Result

Queue: [1, 2, 3]
Removed element: 1
Remove and queue after: [2, 3]

In the above example, we used Java's Queue interface to implement the queue. Here, we use the LinkedList class that implements the Queue interface.

  • Numbers.offer() - Insert an element at the end of the queue

  • Numbers.poll() - Remove an element from the front of the queue

Note that we used angle brackets <Integer> when creating the queue. It indicates that the queue is a generic type. For more information about generics, please visitJava Generics.

We can also use other interfaces and classes to replace Queue and LinkedList. For example,

Java Examples Comprehensive