Queues & Linked List Creating A Queue, Adding Elements N Displaying Result
Queues & Linked List Creating A Queue, Adding Elements N Displaying Result
import java.util.LinkedList;
import java.util.Queue;
// 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("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + waitingQueue);
import java.util.LinkedList;
import java.util.Queue;
waitingQueue.add("Jennifer");
waitingQueue.add("Angelina");
waitingQueue.add("Johnny");
waitingQueue.add("Sachin");
// Get the element at the front of the Queue without removing it using element()
// The element() method throws NoSuchElementException if the Queue is empty
String firstPersonInTheWaitingQueue = waitingQueue.element();
System.out.println("First Person in the Waiting Queue (element()) : " + firstPersonInTheWaitingQueue);
// Get the element at the front of the Queue without removing it using peek()
// The peek() method is similar to element() except that it returns null if the Queue is empty
firstPersonInTheWaitingQueue = waitingQueue.peek();
System.out.println("First Person in the Waiting Queue : " + firstPersonInTheWaitingQueue);
}
}