Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Queue using a LinkedList in Java

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

public class QueueExample {
    public static void main(String[] args) {
        // Create and initialize a Queue using a LinkedList
        Queue<String> waitingQueue = new LinkedList<>();

        // Adding new elements to the Queue (The Enqueue operation)
        waitingQueue.add("Rajeev");
        waitingQueue.add("Chris");
        waitingQueue.add("John");
        waitingQueue.add("Mark");
        waitingQueue.add("Steven");

        System.out.println("Name of Customer : " + waitingQueue);

        // Removing an element from the Queue using remove() (The Dequeue operation)
        // The remove() method throws NoSuchElementException if the Queue is empty
        String name = waitingQueue.remove();
        System.out.println("1st customer got the order : " + name + " | Next Customer : " + waitingQueue);
        
        name = waitingQueue.remove();
        System.out.println("2nd Customer got the order : " + name + " | Next Customer: " + waitingQueue);
        
        name = waitingQueue.remove();
        System.out.println("3nd Customer got the order : " + name + " | Next Customer: " + waitingQueue);
        // Removing an element from the Queue using poll()
        // The poll() method is similar to remove() except that it returns null if the Queue is empty.
        name = waitingQueue.poll();
        System.out.println("4nd Customer got the order : " + name + " | Last Customer: " + waitingQueue);
        
        
    }
}









Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.