A queue is a special linear table that allows for deletion only on the front end of the table, and on the back end of the table.
The LinkedList class implements the queue interface, so we can use LinkedList as a queue.
The following example demonstrates the use of a queue:
Importjava.util.LinkedList;ImportJava.util.Queue; Public classMain { Public Static voidMain (string[] args) {//The Add () and remove () methods throw exceptions when they fail (not recommended)queue<string> queue =NewLinkedlist<string>(); //adding elementsQueue.offer ("a"); Queue.offer ("B"); Queue.offer (C); Queue.offer ("D"); Queue.offer (E); for(String q:queue) {System.out.println (q); } System.out.println ("==="); System.out.println ("Poll=" +queue.poll ());//returns the first element and deletes it in a queue for(String q:queue) {System.out.println (q); } System.out.println ("==="); System.out.println ("Element=" +queue.element ());//returns the first element for(String q:queue) {System.out.println (q); } System.out.println ("==="); System.out.println ("Peek=" +queue.peek ());//returns the first element for(String q:queue) {System.out.println (q); } }}Main.java fileImport Java.Util.LinkedList;Import Java.Util.Queue;Public Class Main { Public Static void Main(String[] Args) { //The Add () and remove () methods throw exceptions when they fail (not recommended) Queue<String>Queue=New LinkedList<String>();//adding elements Queue.Offer("A");Queue.Offer("B");Queue.Offer("C");Queue.Offer("D");Queue.Offer("E");For(String Q:Queue){ System.Out.println(Q);} System.Out.println("===");System.Out.println("Poll="+Queue.Poll());//Returns the first element and deletes it in a queue For(String Q:Queue){ System.Out.println(Q);} System.Out.println("===");System.Out.println("Element="+Queue.Element());//Returns the first element For(String Q:Queue){ System.Out.println(Q);} System.Out.println("===");System.