Donate : Link

WordPress Blog : Link

A Queue is a First In First Out (FIFO) data structure. It models a queue in real-life. Yes, the one that you might have seen in front of a movie theater, a shopping mall, a metro, or a bus.

Just like queues in real life, new elements in a Queue data structure are added at the back and removed from the front. A Queue can be visualized as shown in the figure below.

The process of adding an element at the back of the Queue is called Enqueue, and the process of removing an element from the front of the Queue is called Dequeue.

Java provides a Queue interface which is part of Java’s collections framework. The figure below depicts the position of Queue interface in Collections hierarchy -

A Queue in Java is just an interface. We need a concrete implementation of the Queue interface to work with, in our programs.

As shown in the diagram above, the LinkedList class implements the Queue interface and therefore it can be used as a Queue.

Example 1 :

Output :

Example 2 :

Output :

Example 3 :

Output :