0
0
JavaHow-ToBeginner · 3 min read

How to Implement Queue Using LinkedList in Java

You can implement a Queue in Java using the LinkedList class which implements the Queue interface. Use methods like offer() to add elements and poll() to remove elements in FIFO order.
📐

Syntax

To implement a queue using LinkedList in Java, declare a Queue variable and assign it a new LinkedList object. Use offer() to add elements, poll() to remove the head element, and peek() to view the head without removing it.

  • Queue<Type> queue = new LinkedList<>(); - creates the queue
  • queue.offer(element); - adds element to the queue
  • queue.poll(); - removes and returns the head element
  • queue.peek(); - returns the head element without removing
java
import java.util.Queue;
import java.util.LinkedList;

public class QueueSyntax {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        queue.offer("first");
        queue.offer("second");
        String head = queue.poll();
        String peek = queue.peek();
    }
}
💻

Example

This example shows how to create a queue using LinkedList, add elements, remove them in FIFO order, and peek at the next element.

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

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

        // Add elements to the queue
        queue.offer("Apple");
        queue.offer("Banana");
        queue.offer("Cherry");

        System.out.println("Queue after adding elements: " + queue);

        // Remove elements from the queue
        String removed = queue.poll();
        System.out.println("Removed element: " + removed);

        // Peek at the next element
        String next = queue.peek();
        System.out.println("Next element to remove: " + next);

        System.out.println("Queue now: " + queue);
    }
}
Output
Queue after adding elements: [Apple, Banana, Cherry] Removed element: Apple Next element to remove: Banana Queue now: [Banana, Cherry]
⚠️

Common Pitfalls

Common mistakes when using LinkedList as a queue include:

  • Using add() instead of offer() which throws an exception if the queue is full (rare for LinkedList but important for bounded queues).
  • Using remove() instead of poll() which throws an exception if the queue is empty.
  • Not checking for null when poll() or peek() returns null if the queue is empty.

Correct usage avoids exceptions and handles empty queues gracefully.

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

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

        // Wrong: remove() throws exception if empty
        // queue.remove(); // Throws NoSuchElementException

        // Right: poll() returns null if empty
        Integer val = queue.poll();
        if (val == null) {
            System.out.println("Queue is empty, nothing to remove.");
        }
    }
}
Output
Queue is empty, nothing to remove.
📊

Quick Reference

MethodDescription
offer(E e)Adds element to the queue, returns false if it fails
poll()Removes and returns the head element, or null if empty
peek()Returns the head element without removing, or null if empty
isEmpty()Checks if the queue has no elements
size()Returns the number of elements in the queue

Key Takeaways

Use LinkedList with the Queue interface to implement a FIFO queue in Java.
Use offer() to add elements and poll() to remove elements safely without exceptions.
Always check for null when polling or peeking to handle empty queues gracefully.
Avoid remove() and add() methods if you want to prevent exceptions on empty or full queues.
LinkedList provides an easy and efficient way to implement queues with dynamic size.